-
Notifications
You must be signed in to change notification settings - Fork 70
/
bearer-verify.provider.ts
32 lines (30 loc) · 1.1 KB
/
bearer-verify.provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (c) 2023 Sourcefuse Technologies
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import {inject, Provider} from '@loopback/context';
import {HttpErrors, Request} from '@loopback/rest';
import {ILogger, LOGGER} from '@sourceloop/core';
import {verify} from 'jsonwebtoken';
import {VerifyFunction} from 'loopback4-authentication';
import {SignupRequest} from '../models/signup-request.model';
export class SignupBearerVerifyProvider
implements Provider<VerifyFunction.BearerFn<SignupRequest>>
{
constructor(@inject(LOGGER.LOGGER_INJECT) public logger: ILogger) {}
value(): VerifyFunction.BearerFn<SignupRequest> {
return async (token: string, req?: Request) => {
let result: SignupRequest;
try {
result = verify(token, process.env.JWT_SECRET as string, {
issuer: process.env.JWT_ISSUER,
algorithms: ['HS256'],
}) as SignupRequest;
} catch (error) {
this.logger.error(JSON.stringify(error));
throw new HttpErrors.Unauthorized('TokenExpired');
}
return result;
};
}
}