#Unauthorized

1 messages · Page 1 of 1 (latest)

tall crow
#

When i call any request i get Unauthorized 401 even i call request that returns hello it says me Unauthorized 401

#

I don't know what happens\

#

i fix error

#

problem is in providers: [
{
provide: APP_GUARD,
useClass: AtGuard,
},
],

ancient needle
#

What is AtGuard? Is this making use of passport and the AuthGusrd()?

tall crow
#

I just fix it

tall crow
#

if from fromBodyField() then how can i do that?

ancient needle
#

Check passport's documentation for what extractors exist

tall crow
#

i saw fromBodyField()

#

but how can i do that if i need add field

#

super({
jwtFromRequest: ExtractJwt.fromBodyField(field),
secretOrKey: process.env.REFRESH_SECRET,
passReqToCallback: true,
});

#

where i can take refreshToken from req to Strategy?

tall crow
#

sorry if i disturb you

ancient needle
#

I provide support when possible. I also have a life outside of this server and have plans today, so most likely I'll be sparsely here

dense canopy
# tall crow where i can take refreshToken from req to Strategy?

refresh tokens and a secret are 2 different things
you will have to implement a refresh token system your self

a refresh token is like a master key
a login token is generated with the refresh token as the key

i suggest to save a new refresh token for each newly created user in the database
so when they login with right credentials
the refresh token gets fetched and a new login token is made with the refresh token as the key
this login token is then handed to the user so they are logged in
when the login token is expired a request is made for a new one so they remain logged in

this secretOrKey in the context with refresh tokens should't be A refresh token but a login token

#

also enjoy those plans jmcdo 😊

tall crow
#

do you know about getMe? When we put accesToken into header and etc. And i want realize the same type but not access so refresh

#

and not into header

#

i want to check it for validity

#

because my refreshToken has 60 days and i need to check it

#

for validity

dense canopy
#

the whole point of a refresh token
is a master key in case a user loses their hotel room key

tall crow
#

i want to validate my refreshToken before i work with it

dense canopy
# tall crow i want to validate my refreshToken before i work with it

lets put the example of a hotel

a manager has a master key so they can access all hotel rooms
users get a key so they can access their own
if a user loses a key the manager can open the door for them

now imagen users being their own managers
if the master key expires everybody gets stuck

tall crow
#

i not need help

tall crow
tall crow
tall crow
#

i know how it use without passport library but i don't know how use it with passport

dense canopy
tall crow
dense canopy
#

you're asking something that isn't easy even without
ngl impressed you found a way to validate a master key like that

#

sorry i'm afraid it would stop here for me
i purposely switched away from passport because i hated it

tall crow
tall crow
dense canopy
ancient needle
tall crow
#

I want to do the same type but with refreshToken and i want take refreshToken from body not from header

tall crow
ancient needle
#

So I don't understand what you mean by "do the same... With refresh token"

#

It's just another jwt, with a new secret, right? Use a new strategy, with a new guard, with the proper extractor and it should all be fine

tall crow
#

this is code

#
@Injectable()
export class RtStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
  constructor(prismaService: PrismaService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: process.env.REFRESH_SECRET, 
      passReqToCallback: true,
    });
  }
  private extractTokenFromHeader(request: Request): string | undefined {
    const [type, token] = request.headers.authorization?.split(' ') ?? [];
    return type === 'Bearer' ? token : undefined;
  }

  validate(req: Request, payload: JwtPayload): JwtPayloadWithRt {
    const refreshToken = this.extractTokenFromHeader(req)
 
    if (!refreshToken) throw new ForbiddenException('Refresh token expired');

    return {
      ...payload,
      refreshToken,
    };
  }
}```
tall crow
#

I already do that thank you

#

I do like that

#
@Injectable()
export class RtStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
  private extractTokenFromRequest(request: Request): string | undefined {
    const refreshToken = request.body.refreshToken; 
    return refreshToken;
  }

  constructor(prismaService: PrismaService) {
    super({
      jwtFromRequest: (request: Request) => this.extractTokenFromRequest(request),
      secretOrKey: process.env.REFRESH_SECRET, 
      passReqToCallback: true,
    });

  }

  validate(req: Request, payload: JwtPayload): JwtPayloadWithRt {
    const refreshToken = this.extractTokenFromRequest(req);
 
    if (!refreshToken) throw new ForbiddenException('Refresh token expired');

    return {
      ...payload,
      refreshToken,
    };
  }
}```
ancient needle