Skip to content

Commit e360744

Browse files
committed
removed username as a parameter to refresh token
1 parent b2fce78 commit e360744

File tree

5 files changed

+16
-63
lines changed

5 files changed

+16
-63
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ query user {
250250

251251
```graphql
252252
query refreshToken {
253-
refreshToken(username: "username")
253+
refreshToken
254254
}
255255
```
256256

src/auth/auth.resolvers.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Resolver, Args, Query } from '@nestjs/graphql';
1+
import { Resolver, Args, Query, Context } from '@nestjs/graphql';
22
import { LoginUserInput, LoginResult } from '../graphql.classes';
33
import { AuthService } from './auth.service';
44
import { AuthenticationError } from 'apollo-server-core';
55
import { JwtAuthGuard } from './guards/jwt-auth.guard';
66
import { UseGuards } from '@nestjs/common';
77
import { UsernameEmailGuard } from './guards/username-email.guard';
88
import { UsersService } from '../users/users.service';
9+
import { UserDocument } from '../users/schemas/user.schema';
910

1011
@Resolver('Auth')
1112
export class AuthResolver {
@@ -25,9 +26,9 @@ export class AuthResolver {
2526

2627
// There is no username guard here because if the person has the token, they can be any user
2728
@Query('refreshToken')
28-
@UseGuards(JwtAuthGuard, UsernameEmailGuard)
29-
async refreshToken(@Args('username') username: string): Promise<string> {
30-
const user = await this.usersService.findOneByUsername(username);
29+
@UseGuards(JwtAuthGuard)
30+
async refreshToken(@Context('req') request: any): Promise<string> {
31+
const user: UserDocument = request.user;
3132
if (!user)
3233
throw new AuthenticationError(
3334
'Could not log-in with the provided credentials',

src/auth/auth.types.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type Query {
22
login(user: LoginUserInput!): LoginResult!
3-
refreshToken(username: String!): String!
3+
refreshToken: String!
44
}
55

66
type LoginResult {

src/graphql.classes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export abstract class IMutation {
4444
export abstract class IQuery {
4545
abstract login(user: LoginUserInput): LoginResult | Promise<LoginResult>;
4646

47-
abstract refreshToken(username: string): string | Promise<string>;
47+
abstract refreshToken(): string | Promise<string>;
4848

4949
abstract users(): User[] | Promise<User[]>;
5050

test/users.e2e-spec.ts

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ describe('Users (e2e)', () => {
203203
});
204204

205205
describe('refresh token', () => {
206-
it('works with username', async () => {
206+
it('works', async () => {
207207
const data = {
208-
query: `{refreshToken(username: "uSer1")}`,
208+
query: `{refreshToken}`,
209209
};
210210

211211
await new Promise(resolve => {
@@ -231,41 +231,9 @@ describe('Users (e2e)', () => {
231231
});
232232
});
233233

234-
it('fails for admin on another user', () => {
235-
const data = {
236-
query: `{refreshToken(username: "user1")}`,
237-
};
238-
return request(app.getHttpServer())
239-
.post('/graphql')
240-
.set('Authorization', `Bearer ${adminLogin.token}`)
241-
.send(data)
242-
.expect(200)
243-
.expect(response => {
244-
expect(response.body.errors[0].extensions.code).toEqual(
245-
'UNAUTHENTICATED',
246-
);
247-
});
248-
});
249-
250-
it('fails with wrong username', () => {
251-
const data = {
252-
query: `{refreshToken(username: "uSer10")}`,
253-
};
254-
return request(app.getHttpServer())
255-
.post('/graphql')
256-
.set('Authorization', `Bearer ${user1Login.token}`)
257-
.send(data)
258-
.expect(200)
259-
.expect(response => {
260-
expect(response.body.errors[0].extensions.code).toEqual(
261-
'UNAUTHENTICATED',
262-
);
263-
});
264-
});
265-
266234
it('fails for disabled user', () => {
267235
const data = {
268-
query: `{refreshToken(username: "disabledUser")}`,
236+
query: `{refreshToken}`,
269237
};
270238
return request(app.getHttpServer())
271239
.post('/graphql')
@@ -281,7 +249,7 @@ describe('Users (e2e)', () => {
281249

282250
it('fails for disabled admin', () => {
283251
const data = {
284-
query: `{refreshToken(username: "disabledAdmin")}`,
252+
query: `{refreshToken}`,
285253
};
286254
return request(app.getHttpServer())
287255
.post('/graphql')
@@ -295,25 +263,9 @@ describe('Users (e2e)', () => {
295263
});
296264
});
297265

298-
it('fails with wrong token', () => {
299-
const data = {
300-
query: `{refreshToken(username: "user2")}`,
301-
};
302-
return request(app.getHttpServer())
303-
.post('/graphql')
304-
.set('Authorization', `Bearer ${user1Login.token}`)
305-
.send(data)
306-
.expect(200)
307-
.expect(response => {
308-
expect(response.body.errors[0].extensions.code).toEqual(
309-
'UNAUTHENTICATED',
310-
);
311-
});
312-
});
313-
314-
it('fails with wrong no token', () => {
266+
it('fails with no token', () => {
315267
const data = {
316-
query: `{refreshToken(username: "user1")}`,
268+
query: `{refreshToken}`,
317269
};
318270
return request(app.getHttpServer())
319271
.post('/graphql')
@@ -328,7 +280,7 @@ describe('Users (e2e)', () => {
328280

329281
it('fails with mispelled token', () => {
330282
const data = {
331-
query: `{refreshToken(username: "user1")}`,
283+
query: `{refreshToken}`,
332284
};
333285
return request(app.getHttpServer())
334286
.post('/graphql')
@@ -456,7 +408,7 @@ describe('Users (e2e)', () => {
456408
});
457409
});
458410

459-
it('fails with wrong no token', () => {
411+
it('fails with no token', () => {
460412
const data = {
461413
query: `{user(username:"user10"){username}}`,
462414
};

0 commit comments

Comments
 (0)