Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.

Commit fe8c7c7

Browse files
committed
feat(server): add cookie on social login through graphql endpoint
1 parent 9da1127 commit fe8c7c7

13 files changed

+91
-33
lines changed

server/src/app.module.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { Module } from '@nestjs/common';
22
import { GraphQLModule } from '@nestjs/graphql';
33
import { join } from 'path';
4-
import { AppController } from './app.controller';
5-
import { AppService } from './app.service';
64
import { ApplicantsModule } from './applicants/applicants.module';
75
import { UsersModule } from './users/users.module';
86
import { SuggestionsModule } from './suggestions/suggestions.module';
97
import { ProjectsModule } from './projects/projects.module';
108
import { ProfilesModule } from './profiles/profiles.module';
11-
import { GithubStrategy } from './auth/github.strategy';
129
import { AuthModule } from './auth/auth.module';
13-
import { AuthService } from './auth/auth.service';
14-
import { UsersService } from './users/users.service';
15-
import { PrismaService } from './prisma.service';
1610

1711
@Module({
1812
imports: [
@@ -27,7 +21,7 @@ import { PrismaService } from './prisma.service';
2721
ProfilesModule,
2822
AuthModule
2923
],
30-
controllers: [AppController],
31-
providers: [AppService, PrismaService, UsersService, AuthService, GithubStrategy]
24+
controllers: [],
25+
providers: []
3226
})
3327
export class AppModule {}

server/src/app.service.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Controller, Get, UseGuards, Req } from '@nestjs/common';
2-
import { AppService } from './app.service';
32
import { AuthGuard } from '@nestjs/passport';
3+
import { AuthService } from './auth.service';
44

55
@Controller()
6-
export class AppController {
7-
constructor(private readonly appService: AppService) {}
6+
export class AuthController {
7+
constructor(private readonly authService: AuthService) {}
88

99
@Get('auth/github')
1010
@UseGuards(AuthGuard('github'))
@@ -15,6 +15,6 @@ export class AppController {
1515
@Get('auth/github/callback')
1616
@UseGuards(AuthGuard('github'))
1717
githubAuthRedirect(@Req() req) {
18-
return this.appService.githubLogin(req);
18+
return this.authService.loginWithGithub(req);
1919
}
2020
}

server/src/auth/auth.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { Module } from '@nestjs/common';
22
import { AuthService } from './auth.service';
3+
import { AuthController } from './auth.controller';
4+
import { AuthResolver } from './auth.resolver';
35
import { UsersService } from '../users/users.service';
46
import { UsersModule } from '../users/users.module';
57
import { PassportModule } from '@nestjs/passport';
68
import { PrismaModule } from '../prisma.module';
79

810
@Module({
911
imports: [UsersModule, PassportModule, PrismaModule],
10-
providers: [AuthService, UsersService]
12+
controllers: [AuthController],
13+
providers: [AuthService, AuthResolver, UsersService]
1114
})
1215
export class AuthModule {}

server/src/auth/auth.resolver.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
import { Resolver, Query, Context } from '@nestjs/graphql';
1+
import { UseGuards } from '@nestjs/common';
2+
import { Resolver, Query, Context, Mutation } from '@nestjs/graphql';
23
import { User } from '../users/models/user.model';
3-
import Ctx from '../types/context.type';
4+
import Ctx from '../lib/context.type';
45
import { AuthService } from './auth.service';
6+
import { Profile } from 'passport';
7+
import { LoginOAuthInput } from './dto/loginOAuthInput';
8+
import { OAuthUserResponse } from './dto/oAuthUserResponse';
59

610
@Resolver(() => User)
7-
export class SuggestionsResolver {
11+
export class AuthResolver {
812
constructor(private authService: AuthService) {}
913

1014
@Query(() => User, { nullable: true })
1115
async me(@Context() context: Ctx) {
1216
return context.req.user;
1317
}
1418

19+
/*
20+
@UseGuards(SocialAuthGuard)
21+
@Mutation(() => oAuthUserResponse)
22+
async loginSocial(profile: Profile, input: LoginOAuthInput): Promise<oAuthUserResponse> {
23+
const social = await this.authService.loginSocial(profile, input.provider);
24+
25+
if (social.isError()) {
26+
return [social.value];
27+
}
28+
29+
const authUser = await this.authService.signToken(social.value);
30+
return authUser;
31+
}
32+
*/
33+
1534
@Query(() => User, { nullable: true })
1635
async logout(@Context() context: Ctx) {
17-
// return this.usersService.logout(context);
36+
return this.authService.logout(context);
1837
}
1938
}

server/src/auth/auth.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ export class AuthService {
3434
return user;
3535
}
3636

37+
async loginWithGithub(context) {
38+
if (!context.user) {
39+
return 'No user from GitHub';
40+
}
41+
42+
console.log(context);
43+
// todo set access token
44+
context.res.cookie('token', 'id', cookieOptions);
45+
46+
return context.user;
47+
}
48+
3749
async logout(context) {
3850
context.res.cookie('token', '', { ...cookieOptions, maxAge: 0 });
3951
return null;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Field, InputType } from '@nestjs/graphql';
2+
import { OAuthProvider } from 'common';
3+
4+
@InputType()
5+
export class LoginOAuthInput {
6+
@Field()
7+
accessToken: string;
8+
9+
@Field(() => OAuthProvider)
10+
provider: OAuthProvider;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { User } from '../../users/models/user.model';
2+
import { Field, ObjectType } from '@nestjs/graphql';
3+
4+
@ObjectType()
5+
export class OAuthUserResponse {
6+
@Field(() => User)
7+
user: User;
8+
9+
@Field()
10+
token: string;
11+
}

server/src/auth/github.strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export class GithubStrategy extends PassportStrategy(Strategy) {
2626
await this.authService.validateGitHubUser(user);
2727
} catch (ex) {
2828
console.log(ex);
29-
throw new UnauthorizedException();
29+
return done(new UnauthorizedException(), false);
3030
}
3131

32-
done(null, user);
32+
return done(null, user);
3333
}
3434
}
File renamed without changes.

0 commit comments

Comments
 (0)