Skip to content

Commit

Permalink
feat: add Google sign-in route on server
Browse files Browse the repository at this point in the history
  • Loading branch information
skgndi12 committed Dec 26, 2023
1 parent cbb74a1 commit 9f6be47
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 11 deletions.
52 changes: 44 additions & 8 deletions api/generate/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,41 @@
},
"openapi": "3.0.3",
"paths": {
"/api/v1/google/sign-in": {
"get": {
"operationId": "AuthV1ApiSpec_get_/sign-in",
"tags": [
"Auth"
],
"summary": "Initiate google sign in",
"parameters": [],
"responses": {
"302": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GoogleSignInV1Response"
}
}
}
},
"default": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HttpErrorResponse"
}
}
}
}
}
}
},
"/api/v1/dev/greeting": {
"post": {
"operationId": "DevApiSpec_post_/greeting",
"operationId": "DevV1ApiSpec_post_/greeting",
"tags": [
"Development"
],
Expand Down Expand Up @@ -60,7 +92,7 @@
},
"/api/v1/dev/server-time": {
"get": {
"operationId": "DevApiSpec_get_/server-time",
"operationId": "DevV1ApiSpec_get_/server-time",
"tags": [
"Development"
],
Expand Down Expand Up @@ -130,13 +162,8 @@
},
"components": {
"schemas": {
"GreetingV1Response": {
"GoogleSignInV1Response": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"additionalProperties": false
},
"HttpErrorResponse": {
Expand All @@ -154,6 +181,15 @@
"messages"
]
},
"GreetingV1Response": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"additionalProperties": false
},
"GreetingV1Request": {
"type": "object",
"properties": {
Expand Down
34 changes: 34 additions & 0 deletions api/src/controller/http/auth/auth.v1.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Request, Response, Router } from 'express';

import { AuthService } from '@src/core/services/auth/auth.service';

import { GoogleSignInV1Response } from '@controller/http/auth/response/auth.v1.response';
import { methodNotAllowed } from '@controller/http/handler';

export class AuthV1Controller {
constructor(private readonly service: AuthService) {}

public routes = (): Router => {
const router: Router = Router();
const googlePrefix = '/v1/google';

router
.route(`${googlePrefix}/sign-in`)
.get(this.googleSignIn)
.all(methodNotAllowed);

return router;
};

public googleSignIn = async (
req: Request,
res: Response<GoogleSignInV1Response>
) => {
const referrer = req.get('Referer') ?? req.get('Referrer') ?? null;
const redirectUri = this.service.initiateGoogleSignIn(
req.protocol,
referrer
);
res.redirect(redirectUri);
};
}
1 change: 1 addition & 0 deletions api/src/controller/http/auth/response/auth.v1.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface GoogleSignInV1Response {}
20 changes: 20 additions & 0 deletions api/src/controller/http/auth/schema/auth.v1.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Tspec } from 'tspec';

import { GoogleSignInV1Response } from '@controller/http/auth/response/auth.v1.response';
import { HttpErrorResponse } from '@controller/http/response';

export type AuthV1ApiSpec = Tspec.DefineApiSpec<{
basePath: '/api/v1/google';
tags: ['Auth'];
paths: {
'/sign-in': {
get: {
summary: 'Initiate google sign in';
responses: {
302: GoogleSignInV1Response;
default: HttpErrorResponse;
};
};
};
};
}>;
2 changes: 1 addition & 1 deletion api/src/controller/http/dev/schema/dev.v1.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@controller/http/dev/response/dev.v1.response';
import { HttpErrorResponse } from '@controller/http/response';

export type DevApiSpec = Tspec.DefineApiSpec<{
export type DevV1ApiSpec = Tspec.DefineApiSpec<{
basePath: '/api/v1/dev';
security: 'jwt';
tags: ['Development'];
Expand Down
11 changes: 9 additions & 2 deletions api/src/controller/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { Logger } from 'winston';
import apiSpecification from '@root/generate/openapi.json';
import { name, version } from '@root/package.json';

import { AuthService } from '@src/core/services/auth/auth.service';

import { AuthV1Controller } from '@controller/http/auth/auth.v1.controller';
import { DevV1Controller } from '@controller/http/dev/dev.v1.controller';
import { HealthController } from '@controller/http/health/health.controller';
import { Middleware } from '@controller/http/middleware';
Expand All @@ -22,7 +25,8 @@ export class HttpServer {

constructor(
private readonly logger: Logger,
private readonly config: HttpConfig
private readonly config: HttpConfig,
private readonly authService: AuthService
) {
this.middleware = new Middleware(this.logger);
}
Expand Down Expand Up @@ -65,7 +69,10 @@ export class HttpServer {
};

private getApiRouters = (): express.Router[] => {
const routers = [new DevV1Controller().routes()];
const routers = [
new DevV1Controller().routes(),
new AuthV1Controller(this.authService).routes()
];
return routers;
};

Expand Down

0 comments on commit 9f6be47

Please sign in to comment.