Skip to content

Instantly share code, notes, and snippets.

@sergiodxa
Created July 21, 2021 23:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiodxa/d00110a748a036f5f343f927ec2faa7f to your computer and use it in GitHub Desktop.
Save sergiodxa/d00110a748a036f5f343f927ec2faa7f to your computer and use it in GitHub Desktop.
Remix authentication library usage example
// 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