-
Notifications
You must be signed in to change notification settings - Fork 10
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
23 changed files
with
263 additions
and
101 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
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,3 +1,4 @@ | ||
export * from './get-current-user-by-access-token.query.handler' | ||
export * from './get-current-user-by-refresh-token.query.handler' | ||
export * from './login.query.handler' | ||
export * from './user-logout.query.handler' |
15 changes: 15 additions & 0 deletions
15
src/application/queries/handlers/user-logout.query.handler.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,15 @@ | ||
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs' | ||
import { lastValueFrom } from 'rxjs' | ||
import { LogoutAction } from '../../../domain' | ||
import { UserLogoutQuery } from '../user-logout.query' | ||
|
||
@QueryHandler(UserLogoutQuery) | ||
export class UserLogoutQueryHandler | ||
implements IQueryHandler<UserLogoutQuery, void> | ||
{ | ||
public constructor(protected readonly action: LogoutAction) {} | ||
|
||
public async execute(query: UserLogoutQuery): Promise<void> { | ||
return lastValueFrom(this.action.handle(query)) | ||
} | ||
} |
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,3 +1,4 @@ | ||
export * from './get-current-user-by-access-token.query' | ||
export * from './get-current-user-by-refresh-token.query' | ||
export * from './login.query' | ||
export * from './user-logout.query' |
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,7 @@ | ||
import { ExecutionContext } from '@nestjs/common' | ||
|
||
export class UserLogoutQuery { | ||
public constructor( | ||
public context: ExecutionContext | ||
) {} | ||
} |
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,4 +1,5 @@ | ||
export * from './get-user-by-jwt-token.action' | ||
export * from './get-user-by-refresh-token.action' | ||
export * from './local-login.action' | ||
export * from './logout.action' | ||
export * from './parse-jwt-token.action' |
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,99 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common' | ||
import { EventBus } from '@nestjs/cqrs' | ||
import { GqlExecutionContext } from '@nestjs/graphql' | ||
import moment from 'moment/moment' | ||
import { map, Observable, of, tap } from 'rxjs' | ||
import { InjectAuthDefinitions } from '../decorators' | ||
import { | ||
IAuthDefinitions, | ||
IAuthUserEntityForResponse, | ||
IRequestWithUser, | ||
} from '../definitions' | ||
import { UserLoggedOutEvent } from '../events' | ||
import { | ||
getRequestFromContext, | ||
getRequestHeader, | ||
getResponseFromContext, | ||
ICookieSerializeOptions, | ||
IHttpResponse, | ||
removeBearerFromToken, | ||
} from '../helpers' | ||
|
||
export interface ILogoutActionOptions { | ||
context: ExecutionContext | ||
} | ||
|
||
@Injectable() | ||
export class LogoutAction { | ||
public constructor( | ||
@InjectAuthDefinitions() | ||
protected readonly definitions: IAuthDefinitions, | ||
protected readonly eventBus: EventBus | ||
) {} | ||
|
||
public handle({ context }: ILogoutActionOptions): Observable<void> { | ||
const res: IHttpResponse = { | ||
...getResponseFromContext(context), | ||
httpAdaptorType: this.definitions.httpAdaptorType, | ||
} | ||
|
||
const request = getRequestFromContext(context) | ||
|
||
const user = getUserFromContext(context) | ||
|
||
const accessToken = removeBearerFromToken( | ||
getRequestHeader(request, 'authorization') as unknown as string | ||
) | ||
|
||
return of(res).pipe( | ||
tap(() => { | ||
this.clearAuthCookies(res) | ||
|
||
this.eventBus.publish(new UserLoggedOutEvent(accessToken, user)) | ||
}), | ||
map(() => undefined) | ||
) | ||
} | ||
|
||
public clearAuthCookies(res: IHttpResponse): void { | ||
if ( | ||
(res.httpAdaptorType === 'fastify' && !res.setCookie) || | ||
(res.httpAdaptorType === 'express' && !res.cookie) | ||
) { | ||
return | ||
} | ||
|
||
const cookieOptions: ICookieSerializeOptions = { | ||
path: '/', | ||
httpOnly: true, | ||
sameSite: 'none', | ||
secure: process.env.NODE_ENV !== 'local', | ||
|
||
...this.definitions.cookieOptions, | ||
|
||
expires: moment().toDate(), | ||
} | ||
|
||
if (res.httpAdaptorType === 'fastify' && res.setCookie) { | ||
res.setCookie('AccessToken', '', cookieOptions) | ||
res.setCookie('RefreshToken', '', cookieOptions) | ||
} | ||
|
||
if (res.httpAdaptorType === 'express' && res.cookie) { | ||
res.cookie('AccessToken', '', cookieOptions) | ||
res.cookie('RefreshToken', '', cookieOptions) | ||
} | ||
} | ||
} | ||
|
||
function getUserFromContext(context: ExecutionContext): IAuthUserEntityForResponse { | ||
if (`${context.getType()}` === 'graphql') { | ||
const gqlExecutionContext = GqlExecutionContext.create(context) | ||
|
||
return gqlExecutionContext.getContext().req.user?.userData | ||
} | ||
|
||
const request: IRequestWithUser = context.switchToHttp().getRequest() | ||
|
||
return request.user?.userData | ||
} |
4 changes: 1 addition & 3 deletions
4
src/domain/events/access-token-generated-from-refresh-token.event.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
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,2 +1,3 @@ | ||
export * from './access-token-generated-from-refresh-token.event' | ||
export * from './user-logged-in.event' | ||
export * from './user-logged-out.event' |
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 { IAuthUserEntityForResponse } from '../definitions' | ||
|
||
export class UserLoggedOutEvent { | ||
public constructor( | ||
public readonly currentAccessToken: string, | ||
public readonly user: IAuthUserEntityForResponse | ||
) { | ||
} | ||
} |
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,3 +1,4 @@ | ||
export * from './hide-redacted-fields.helper' | ||
export * from './http-context.helper' | ||
export * from './remove-bearer-from-token.helper' | ||
export * from './validate-entity' |
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,12 @@ | ||
export function removeBearerFromToken( | ||
token: string, | ||
apiTokenPrefix: string = 'bearer ' | ||
): string { | ||
if (!token) return token | ||
|
||
if (token.toLowerCase().startsWith(apiTokenPrefix)) { | ||
return token.substring(apiTokenPrefix.length) | ||
} | ||
|
||
return token | ||
} |
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
Oops, something went wrong.