forked from yinMrsir/chunyu-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
101 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtWebAuthGuard extends AuthGuard('jwt-web') { | ||
canActivate(context: ExecutionContext) { | ||
return super.canActivate(context); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Nest-server/src/modules/actor/actor-list/dto/update-actor-list.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Nest-server/src/modules/web/auth/strategies/jwt-web.strategies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { jwtConstants } from '../../../system/auth/auth.constants'; | ||
|
||
@Injectable() | ||
export class JwtWebStrategies extends PassportStrategy(Strategy, 'jwt-web') { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: jwtConstants.secret, | ||
passReqToCallback: true, //设置回调的第一个参数是 request | ||
}); | ||
} | ||
|
||
async validate(request: Request, payload) { | ||
const { userId } = payload; | ||
return { userId }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LocalWebStrategy } from './strategies/local-web.strategy'; | ||
import { JwtWebStrategies } from './strategies/jwt-web.strategies'; | ||
import { WebAuthService } from './web-auth.service'; | ||
import { WebUserModule } from '../user/web-user.module'; | ||
|
||
@Module({ | ||
imports: [WebUserModule], | ||
providers: [LocalWebStrategy, WebAuthService], | ||
providers: [LocalWebStrategy, JwtWebStrategies, WebAuthService], | ||
}) | ||
export class WebAuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import {SearchParams} from "ohmyfetch"; | ||
|
||
export const useGet = <T>(url: string, params?: SearchParams): Promise<T> => { | ||
export const useGet = <T>(url: string, params?: SearchParams, headers?: any): Promise<T> => { | ||
const { baseUrl } = useRuntimeConfig() | ||
return $fetch(baseUrl + url, { | ||
method: 'get', | ||
params | ||
params, | ||
headers | ||
}) | ||
} | ||
|
||
export const usePost = <T>(url: string, body?: Record<string, any>) => { | ||
export const usePost = <T>(url: string, body?: Record<string, any>, headers?: any): Promise<T> => { | ||
const { baseUrl } = useRuntimeConfig() | ||
return $fetch(baseUrl + url, { | ||
method: 'post', | ||
body | ||
body, | ||
headers | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {useGet} from "~/composables/useHttp"; | ||
|
||
export default defineEventHandler((event) => { | ||
return useGet<{data: any}>('/web/user/info', {}, { Authorization: event.context.Authorization }) | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default defineEventHandler((event) => { | ||
let userInfo = getCookie(event, 'userInfo') || undefined | ||
if (userInfo) { | ||
event.context.Authorization = 'Bearer ' + JSON.parse(userInfo).token | ||
} | ||
}) |