mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2026-01-03 12:34:23 +00:00
add auth docs, add eslint import/order
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Inject, Injectable } from '@nestjs/common'
|
||||
import { PassportStrategy } from '@nestjs/passport'
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt'
|
||||
|
||||
import { AppSettings } from '../../../settings/app-settings'
|
||||
|
||||
type JwtPayload = {
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
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'
|
||||
import { Strategy } from 'passport-jwt'
|
||||
|
||||
import { AppSettings } from '../../../settings/app-settings'
|
||||
import { UsersService } from '../../users/services/users.service'
|
||||
|
||||
const cookieExtractor = function (req: Request) {
|
||||
let token = null
|
||||
|
||||
if (req && req.cookies) {
|
||||
token = req.cookies['refreshToken']
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'
|
||||
import { PassportStrategy } from '@nestjs/passport'
|
||||
import { Request as RequestType } from 'express'
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt'
|
||||
import { AuthService } from '../auth.service'
|
||||
|
||||
import { AppSettings } from '../../../settings/app-settings'
|
||||
import { UsersService } from '../../users/services/users.service'
|
||||
import { Request as RequestType } from 'express'
|
||||
import { AuthService } from '../auth.service'
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
@@ -25,9 +26,11 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
|
||||
async validate(payload: any) {
|
||||
const user = await this.userService.getUserById(payload.userId)
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException()
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
@@ -35,6 +38,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
if (req.cookies && 'accessToken' in req.cookies && req.cookies.accessToken.length > 0) {
|
||||
return req.cookies.accessToken
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common'
|
||||
import { PassportStrategy } from '@nestjs/passport'
|
||||
import { Strategy } from 'passport-local'
|
||||
|
||||
import { AuthService } from '../auth.service'
|
||||
|
||||
@Injectable()
|
||||
@@ -13,9 +14,11 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
|
||||
|
||||
async validate(email: string, password: string): Promise<any> {
|
||||
const newCredentials = await this.authService.checkCredentials(email, password)
|
||||
|
||||
if (newCredentials.resultCode === 1) {
|
||||
throw new UnauthorizedException('Invalid credentials')
|
||||
}
|
||||
|
||||
return newCredentials
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user