Created
July 21, 2021 23:25
-
-
Save sergiodxa/d00110a748a036f5f343f927ec2faa7f to your computer and use it in GitHub Desktop.
Remix authentication library usage example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a session storage instance | |
let sessionStorage = createCookieSessionStorage(); | |
// create authenticator instance | |
let authenticator = new Authenticator<User>( | |
sessionStorage | |
); | |
// configure the authenticator to use the Auth0 strategy for sign-in | |
authenticator.use( | |
new Auth0Strategy( | |
{ | |
domain: process.env.AUTH0_DOMAIN as string, | |
clientID: process.env.AUTH0_CLIENT_ID as string, | |
clientSecret: process.env.AUTH0_CLIENT_SECRET as string, | |
callbackURL: process.env.AUTH0_CALLBACK_URL as string, | |
}, | |
(_accessToken, _refreshToken, _extraParams, profile) => { | |
User.findOrCreate({ email: profile.emails[0].value }, function (error, user) { | |
if (error) throw error | |
if (!user) throw new Error("User not found") | |
return user; | |
}); | |
} | |
), | |
"auth0" | |
); | |
// User must be logged-in to continue | |
export let privateLoader: LoaderFunction = ({ request }) => { | |
return authenticator.authenticate("auth0", request, async (user) => { | |
return json({ user }); | |
}); | |
}; | |
// User may not be logged-in to continue | |
export let publicLoader: LoaderFunction = async ({ request }) => { | |
let user = await authenticator.isAutenticated(request); | |
if (!user) return json({ error: "Not authorized" }); | |
return json({ user }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment