-
I have simple authentication using express session. But I only found a way to handle an API and throw an error when user is not authenticated in context function. But how I can redirect users to the login page when session data is missing when accessing playground? |
Beta Was this translation helpful? Give feedback.
Answered by
jcubic
Apr 3, 2023
Replies: 1 comment
-
Ok found that import { Request, Response, NextFunction } from 'express';
export function is_admin(req: Request, res: Response, next: NextFunction) {
if (!req.session.admin && !DEBUG) {
return res.redirect(302, `${ADMIN_LOGIN}?next=${next_url(req)}`);
}
next();
}
export const start = async (port: number, callback: () => void) => {
const httpServer = http.createServer(app);
const server = apollo_server(httpServer);
await server.start();
app.use(
'/api/',
cors<cors.CorsRequest>({ origin: "*" }),
json(),
is_admin,
expressMiddleware(server, {
context: create_context
})
);
httpServer.listen({ port }, callback);
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
jcubic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok found that
app.use()
accepts any number of middlewares, so I can use add myis_admin
middleware to the list: