Hi Michael, How to store the user info in cookie session in Nest? Could you point me to the right direction?
Hi, I think this example is for jwt. If you want to use a cookie session you can add a route guard that creates a session cookie for you.
Controller
@Post('/signin') @UseGuards(LoginGuard) signin(@Request() req: IUserRequest): void { console.log(`@AuthController ${JSON.stringify(req.user)}`) }
Guard
import { ExecutionContext, Injectable } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; @Injectable() export class LoginGuard extends AuthGuard('local') { async canActivate(context: ExecutionContext): Promise<boolean> { const result = (await super.canActivate(context)) as boolean; const request = context.switchToHttp().getRequest(); await super.logIn(request); return result; } }
At this point you have a cookie and you can redirect the user back to front end ( if you have one ) and do another request to ask user information.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Michael,
How to store the user info in cookie session in Nest? Could you point me to the right direction?
Hi, I think this example is for jwt.
If you want to use a cookie session you can add a route guard that creates a session cookie for you.
Controller
Guard
At this point you have a cookie and you can redirect the user back to front end ( if you have one ) and do another request to ask user information.