Skip to content

Commit

Permalink
fix: catch authorization error with proper error message
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed May 25, 2022
1 parent 5812853 commit b94f7af
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions worker/context/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Authenticator } from 'remix-auth';
import { Authenticator, AuthorizationError } from 'remix-auth';
import { GitHubStrategy } from 'remix-auth-github';
import { createCookieSessionStorage, redirect } from '@remix-run/cloudflare';
import type { Env, MessageType, SessionData, UserProfile } from '../types';
Expand Down Expand Up @@ -31,6 +31,7 @@ export function createSession(

let authenticator = new Authenticator<UserProfile>(sessionStorage, {
sessionErrorKey: 'message',
throwOnError: true,
});

if (
Expand Down Expand Up @@ -60,7 +61,11 @@ export function createSession(
email: profile.emails[0].value,
};

await userStore.updateProfile(userProfile);
try {
await userStore.updateProfile(userProfile);
} catch (e) {
console.log('Fail updating user profile:', e);
}

return userProfile;
},
Expand All @@ -69,20 +74,30 @@ export function createSession(

return {
async login(): Promise<void> {
const user = await authenticator.authenticate('github', request, {
failureRedirect: '/',
});
const session = await sessionStorage.getSession(
request.headers.get('cookie'),
);
try {
const user = await authenticator.authenticate('github', request);
const session = await sessionStorage.getSession(
request.headers.get('cookie'),
);

session.set(authenticator.sessionKey, user);
session.set(authenticator.sessionKey, user);

throw redirect(`/${user.name ?? ''}`, {
headers: {
'Set-Cookie': await sessionStorage.commitSession(session),
},
});
throw redirect(`/${user.name ?? ''}`, {
headers: {
'Set-Cookie': await sessionStorage.commitSession(session),
},
});
} catch (ex) {
if (ex instanceof AuthorizationError) {
throw redirect('/', {
headers: {
'set-cookie': await this.flash(ex.message, 'error'),
},
});
}

throw ex;
}
},
async logout(): Promise<void> {
await authenticator.logout(request, {
Expand Down

0 comments on commit b94f7af

Please sign in to comment.