Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize and deserialze sessions with email and userType #804

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/server/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { Express } from 'express';
import passport from 'passport';
import { User, UserType } from '../generated/graphql';
import { Models } from '../models';
import { fetchUser } from '../resolvers/helpers';

export interface StrategyNames {
displayName: string;
name: string;
scopes: string[];
}

export const registerAuthRoutes = (app: Express, strategies: StrategyNames[]): void => {
passport.serializeUser((user, done) => void done(null, user));
passport.deserializeUser((user, done) => void done(null, user));
export const registerAuthRoutes = (
app: Express,
strategies: StrategyNames[],
models: Models
): void => {
passport.serializeUser(({ email, userType }: User, done) => {
void done(null, { email, userType });
});
passport.deserializeUser(
async ({ email, userType }: { email: string; userType: UserType }, done) => {
done(null, await fetchUser({ email, userType }, models));
}
);

strategies.forEach(({ name: authName, scopes }) => {
app.get(`/api/auth/${authName}`, passport.authenticate(authName, { scope: scopes }));
Expand Down
2 changes: 1 addition & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const schema = makeExecutableSchema({
// console.error(config); // sanity check for auth plugin
});

registerAuthRoutes(app, oAuthStrategies);
registerAuthRoutes(app, oAuthStrategies, models);

app.use((req, res, next) =>
passport.authenticate(
Expand Down