Im trying to implement google oauth on Express serverless using TS and library routing-controller. I have modularized each part of the code To authenticate im using passport library and passport-google-oauth-20
google.controller.ts file
import passport from 'passport';
import { Service } from 'typedi';
import { Get, JsonController, Req, Res } from 'routing-controllers';
@JsonController('/auth-google')
@Service()
export class GoogleController {
constructor() {}
@Get()
async googleAuth(@Req() req: any, @Res() res: any)
{
try
{
console.log('googleAuth', req)
passport.authenticate('google', { scope: ['profile', 'email'] })
}
catch (error) {
console.log('error en seleccion de cuenta ', error)
}
}
@Get('/redirect')
async googleAuthRedirect(@Req() req: any, @Res() res: any) {
try {
console.log('googleAuthRedirectvqedfqe v', req)
return passport.authenticate('google', { failureRedirect: '/api' });
} catch (error) {
console.log('error en redirect', error)
}
}
}
This file is meant to authenticate the user. The first step is to redirect the user to the google selection account screen and then with the info from the selected account check if the user is already on the database. Instead it returns a 404
app.ts
import 'reflect-metadata'
import * as dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import session from 'express-session'
import helmet from 'helmet'
import passport from 'passport'
import { useContainer, useExpressServer } from 'routing-controllers'
import { Container } from 'typedi'
import { GoogleController } from './auth/google/google.controller'
import { CatchAllMiddleware } from './middleware/catchAllRoutes'
import { ErrorMiddleware } from './middleware/errorTransformer'
import { UserController } from './user/user.controller'
import { FacebookController } from './auth/facebook/facebook.controller'
let app: express.Application
// eslint-disable-next-line prettier/prettier
(async () => {
app = express()
app.use(express.json())
app.use(helmet())
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}),
)
app.use(passport.initialize())
app.use(passport.session())
useContainer(Container)
useExpressServer(app, {
controllers: [UserController, GoogleController, FacebookController],
middlewares: [ErrorMiddleware, CatchAllMiddleware],
defaultErrorHandler: false,
cors: true,
routePrefix: '/api',
validation: true,
})
})()
export { app }
console.log('googleAuth', req) is returning a request but instead of showing google selection account screen its returning 404 i dont understand why its returning a 404 code status if im trying to reach http://localhost:3000/api/auth-google that is a valid path
I've check paths a thousand times but its not working I've try installing al npm dependencies again but i don't know what else to try, i even ask chat gtp to help but the responses are vague or maybe im not being clear. User path works and its declared the same way the auth paths are declared. Please help