Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Use session UUID as refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 22, 2020
1 parent 0e605fe commit 34eb080
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default () => ({
security: {
saltRounds: process.env.SALT_ROUNDS ?? 10,
jwtSecret: process.env.JWT_SECRET ?? 'staart',
accessTokenExpiry: process.env.ACCESS_TOKEN_EXPIRY ?? '1h',
},
email: {
name: process.env.EMAIL_NAME ?? 'Staart',
Expand Down
10 changes: 7 additions & 3 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Headers, Ip, Post, UseGuards } from '@nestjs/common';
import { users } from '@prisma/client';
import { RateLimit } from 'nestjs-rate-limiter';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
Expand All @@ -16,8 +16,12 @@ export class AuthController {
duration: 60,
errorMessage: 'Wait for 60 seconds before trying to login again',
})
async login(@Body() data: LoginDto): Promise<{ accessToken: string }> {
return this.authService.login(data.email, data.password);
async login(
@Body() data: LoginDto,
@Ip() ip: string,
@Headers('') userAgent: string,
): Promise<{ accessToken: string }> {
return this.authService.login(ip, userAgent, data.email, data.password);
}

@Post('register')
Expand Down
3 changes: 1 addition & 2 deletions src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { JwtStrategy } from './jwt.strategy';
EmailModule,
ConfigModule,
JwtModule.register({
secret: process.env.JWT_SECRET,
signOptions: { expiresIn: '60s' },
secret: process.env.JWT_SECRET ?? 'staart',
}),
],
controllers: [AuthController],
Expand Down
23 changes: 20 additions & 3 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UsersService } from '../user/user.service';
import { RegisterDto } from './auth.dto';
import { compare } from 'bcrypt';
import { JwtService } from '@nestjs/jwt';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -40,12 +41,28 @@ export class AuthService {
return null;
}

async login(email: string, password?: string) {
async login(
ipAddress: string,
userAgent: string,
email: string,
password?: string,
) {
const id = await this.validateUser(email, password);
if (!id) throw new UnauthorizedException();
const payload = { sub: id };
const token = randomStringGenerator();
await this.prisma.sessions.create({
data: { token, ipAddress, userAgent, user: { connect: { id } } },
});
return {
accessToken: this.jwtService.sign(payload),
accessToken: this.jwtService.sign(
{ sub: `user${id}` },
{
expiresIn: this.configService.get<string>(
'security.accessTokenExpiry',
),
},
),
refreshToken: token,
};
}

Expand Down

0 comments on commit 34eb080

Please sign in to comment.