I'm trying to integrate with my NodeJS api developed in typescript with google Oauth2 through passport, but following the documentation that is in javascript, I have an underlined error in GoogleStrategy
This expression is not constructable.
Type 'typeof import("/home/mypc/networking_api/node_modules/@types/passport-google-oauth20/index")' has no construct signatures.
import GoogleStrategy from 'passport-google-oauth20';
import passport from 'passport';
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET ,
callbackURL: "/auth/google/callback"
},
function({accessToken, refreshToken, profile, cb}: any) {
}
));
Also, because it is typescript, in the accessToken, refreshToken and etc variables, I would need to put a type and so I put "any". Would it be correct?
CodePudding user response:
Try it like this:
import {Strategy as GoogleStrategy } from 'passport-google-oauth20';
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET ,
callbackURL: "/auth/google/callback"
},
function({accessToken, refreshToken, profile, cb}: any) {
}
));
To not use any define a interface.
Update:
To create enviroments variables do like so:
- create a .env file in your root project folder
- npm i dotenv
- create enviroments variables in the .env file
clientId=1234
clientSecret=mysupersecret
Add following to your code:
import * as dotenv from 'dotenv'
dotenv.config()
now you can access the env variables with:
process.env.clientId