-
Notifications
You must be signed in to change notification settings - Fork 0
/
siwe.strategy.ts
33 lines (31 loc) · 980 Bytes
/
siwe.strategy.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
33
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { JWT_SECRET, SIWE_STRATEGY_NAME } from './siwe.constants';
import { PrismaService } from '@/module/prisma/prisma.service';
import { CurrentOpSignUser, JwtPayload } from './siwe.type';
@Injectable()
export class SiweJwtStrategy extends PassportStrategy(
Strategy,
SIWE_STRATEGY_NAME,
) {
constructor(private readonly prisma: PrismaService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: JWT_SECRET,
});
}
async validate({ address }: JwtPayload): Promise<CurrentOpSignUser | null> {
if (!address) return null;
const user = await this.prisma.opSignUser.findUnique({
where: { address },
select: {
address: true,
},
});
return user as {
address: `0x${string}`;
} | null;
}
}