resend email in progress

This commit is contained in:
2023-06-14 16:13:22 +02:00
parent 6f2fab076d
commit 612b2326f9
17 changed files with 181 additions and 164 deletions

View File

@@ -0,0 +1,34 @@
import { Inject, Injectable } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { Strategy } from 'passport-jwt'
import { UsersService } from '../../users/services/users.service'
import { AppSettings } from '../../../settings/app-settings'
import { Request } from 'express'
const cookieExtractor = function (req: Request) {
console.log(req.cookies)
let token = null
if (req && req.cookies) {
token = req.cookies['refreshToken']
}
console.log(token)
return token
}
// ...
@Injectable()
export class JwtRefreshStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
constructor(
@Inject(AppSettings.name) private readonly appSettings: AppSettings,
private userService: UsersService
) {
super({
jwtFromRequest: cookieExtractor,
ignoreExpiration: true,
secretOrKey: appSettings.auth.REFRESH_JWT_SECRET_KEY,
})
}
async validate(payload: any) {
return this.userService.getUserById(payload.userId)
}
}

View File

@@ -1,12 +1,17 @@
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { AuthService } from '../auth.service'
import { AppSettings } from '../../../settings/app-settings'
import { Request } from 'express'
import { UsersService } from '../../users/services/users.service'
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(@Inject(AppSettings.name) private readonly appSettings: AppSettings) {
constructor(
@Inject(AppSettings.name) private readonly appSettings: AppSettings,
private authService: AuthService,
private userService: UsersService
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
@@ -14,29 +19,12 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
})
}
async validate(request: Request, payload: any) {
const accessToken = request.headers.authorization?.split(' ')[1]
const refreshToken = request.cookies.refreshToken // Extract refresh token from cookies
// If there's no refresh token, simply validate the user based on payload
if (!refreshToken) {
return { userId: payload.userId }
}
try {
const newAccessToken = await this.authService.checkToken(accessToken, refreshToken)
// If new access token were issued, attach it to the response headers
if (newAccessToken) {
request.res.setHeader('Authorization', `Bearer ${newAccessToken.accessToken}`)
}
request.res.cookie('refreshToken', newAccessToken.refreshToken, {
httpOnly: true,
path: '/auth/refresh-token',
})
return { userId: payload.userId }
} catch (error) {
throw new UnauthorizedException('Invalid tokens')
async validate(payload: any) {
console.log(payload)
const user = await this.userService.getUserById(payload.userId)
if (!user) {
throw new UnauthorizedException()
}
return user
}
}

View File

@@ -12,10 +12,10 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
}
async validate(email: string, password: string): Promise<any> {
const credentials = await this.authService.checkCredentials(email, password)
if (credentials.resultCode === 1) {
const newCredentials = await this.authService.checkCredentials(email, password)
if (newCredentials.resultCode === 1) {
throw new UnauthorizedException()
}
return credentials
return newCredentials
}
}

View File

@@ -1,27 +0,0 @@
import { Inject, Injectable } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { AppSettings } from '../../../settings/app-settings'
import { Request } from 'express'
type JwtPayload = {
userId: string
username: string
}
@Injectable()
export class RefreshTokenStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
constructor(@Inject(AppSettings.name) private readonly appSettings: AppSettings) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: appSettings.auth.ACCESS_JWT_SECRET_KEY,
passReqToCallback: true,
})
}
validate(req: Request, payload: any) {
const refreshToken = req.get('Authorization').replace('Bearer', '').trim()
return { ...payload, refreshToken }
}
}