feat: magic-link auth complete

This commit is contained in:
Balu Babu
2023-01-10 16:06:42 +05:30
parent 0c154be04e
commit fc284fd0a2
17 changed files with 6590 additions and 65 deletions

View File

@@ -0,0 +1,61 @@
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import {
Injectable,
ForbiddenException,
UnauthorizedException,
} from '@nestjs/common';
import { AccessTokenPayload } from 'src/types/AuthTokens';
import { UserService } from 'src/user/user.service';
import { AuthService } from '../auth.service';
import { Request } from 'express';
import * as O from 'fp-ts/Option';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor(
private usersService: UserService,
private authService: AuthService,
) {
super({
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
const ATCookie = request.cookies['access_token'];
if (!ATCookie) {
throw new ForbiddenException('No cookies present');
}
return ATCookie;
},
]),
secretOrKey: process.env.JWT_SECRET,
});
}
async validate(payload: AccessTokenPayload) {
if (!payload) throw new ForbiddenException('Access token malformed');
const user = await this.usersService.findUserById(payload.sub);
if (O.isNone(user)) {
throw new UnauthorizedException('User not found');
}
const profile = {
provider: 'email',
id: user.value.email,
};
const providerAccountExists =
await this.authService.checkIfProviderAccountExists(user.value, profile);
if (!providerAccountExists)
await this.usersService.createProviderAccount(
user.value,
null,
null,
profile,
);
return user.value;
}
}