thiagobustamante / typescript-rest

This is a lightweight annotation-based expressjs extension for typescript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PassportJS with Google OAuth strategy not working as expected

mateo-m opened this issue · comments

commented

Hello everybody!
First time creating an issue on GitHub, sorry if I'm not doing it the right way.

I'm having trouble integrating PassportJS strategies to my project while using typescript-rest.
Using PassportJS the default way (= app.get('/auth/google', passport.authenticate('google'))) redirects me to Google's auth page as expected.

Now when I try to do the same thing with typescript-rest, nothing happens neither in the console or in the page itself.
Here's what I tried with typescript-rest.

Project entry file : index.ts

import express from 'express';
import compression from 'compression';
import helmet from 'helmet';
import { Server } from 'typescript-rest';
import passport from 'passport';
import { API_PORT, API_URL, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET } from './constants';
import appControllers from './controllers';

const GoogleStrategy = require('passport-google-oauth20');

// Express
const app: express.Application = express();

// Passport setup
passport.serializeUser((user, done) => {
  done(null, user);
});

passport.deserializeUser((user: any, done) => {
  done(null, user);
});

// Passport strategies
passport.use(
  new GoogleStrategy(
    {
      clientID: GOOGLE_CLIENT_ID,
      clientSecret: GOOGLE_CLIENT_SECRET,
      callbackURL: `/auth/google/redirect`,
    },
    (/* accessToken: any, refreshToken: any, profile: any, cb: any */) => {
      console.log('test');
      // User.findOrCreate({ googleId: profile.id }, (err, user) => cb(err, user));
    },
  ),
);

app.use(passport.initialize());

// Middlewares
// CORS
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

app.use(compression());
app.use(helmet());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

// typescript-rest
Server.buildServices(app, ...appControllers);

app.listen(API_PORT, () => {
  console.log(`⚡️ [server]: Server is running at ${API_URL}:${API_PORT}`);
});

Controllers list : controllers.ts

import AuthController from './auth/auth.controller';

const appControllers = [AuthController];

export default appControllers;

Auth controller : auth.controller.ts

import AuthService from '@services/auth/auth.service';
import { Path, GET, Accept } from 'typescript-rest';

@Accept('application/json')
@Path('auth')
class AuthController {
  private authService: AuthService;

  getName(): string {
    return 'AuthController';
  }

  constructor() {
    this.authService = new AuthService();
  }

  @Path('google')
  @GET
  googleAuth() {
    return this.authService.google();
  }
}

export default AuthController;

Auth service : auth.service.ts

import passport from 'passport';

class AuthService {
  google() {
    return passport.authenticate('google', {
      scope: ['profile'],
    });
  }
}

export default AuthService;

I simply don't get why app.get() works while everything else I tried fails.
I'm probably doing something wrong, but I can't find out what.