I am trying to use oAuth to work with googleapi
I am using .env to keep secret variables hidden with .gitignore
My problem is that if I try to define
CLIENT_ID = process.env.CLIENT_ID
Outside of the function as a global variable, when I run the variable and console log it, it returns undefined.
However, if I try to define it outside of the function and console log it inside it shows undefined
If I set my controller file like this it works
const passport = require('passport')
const validator = require('validator')
const User = require('../models/User')
const Company = require('../models/Company')
const { google } = require('googleapis');
const { OAuth2Client } = require('google-auth-library');
const axios = require('axios');
const auth = require('../middleware/auth');
const dayjs = require('dayjs')
exports.googleAuthCode = (req, res) => {
let CLIENT_ID = process.env.CLIENT_ID;
let CLIENT_SECRET = process.env.CLIENT_SECRET;
let REDIRECT_URL = process.env.REDIRECT_URL;
const SCOPES = 'https://www.googleapis.com/auth/calendar';
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL,
);
console.log('running googleAuthCode')
console.log('process.env.CLIENT_ID ', process.env.CLIENT_ID)
console.log('process.env.CLIENT_SECRET ', process.env.CLIENT_SECRET)
console.log('process.env.REDIRECT_URL ', process.env.REDIRECT_URL)
console.log('CLIENT_ID ', CLIENT_ID)
console.log('CLIENT_SECRET ', CLIENT_SECRET)
console.log('REDIRECT_URL ', REDIRECT_URL)
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('authURL: ',authUrl)
res.redirect(authUrl);
}
If I set my controller like this it doesn't work:
const passport = require('passport')
const validator = require('validator')
const User = require('../models/User')
const Company = require('../models/Company')
const { google } = require('googleapis');
const { OAuth2Client } = require('google-auth-library');
const axios = require('axios');
const auth = require('../middleware/auth');
const dayjs = require('dayjs')
let CLIENT_ID = process.env.CLIENT_ID;
let CLIENT_SECRET = process.env.CLIENT_SECRET;
let REDIRECT_URL = process.env.REDIRECT_URL;
const SCOPES = 'https://www.googleapis.com/auth/calendar';
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL,
);
exports.googleAuthCode = (req, res) => {
console.log('running googleAuthCode')
console.log('process.env.CLIENT_ID ', process.env.CLIENT_ID)
console.log('process.env.CLIENT_SECRET ', process.env.CLIENT_SECRET)
console.log('process.env.REDIRECT_URL ', process.env.REDIRECT_URL)
console.log('CLIENT_ID ', CLIENT_ID)
console.log('CLIENT_SECRET ', CLIENT_SECRET)
console.log('REDIRECT_URL ', REDIRECT_URL)
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('authURL: ',authUrl)
res.redirect(authUrl);
}
I am using dotenv
I am using MVC setup,
require('dotenv').config({path: './config/.env'})
Is in my server.js file and exported to my controller file.
If you can shed some light on to how i can get this working as a global variable it would be very much appreciated.
CodePudding user response:
Make sure that dotenv
is imported above all other modules.
Assume that I have two files inside my project: main.js
and utils.js
.
// main.js
const { func1 } = require('./utils.js');
require('dotenv').config();
func1();
// utils.js
console.log(process.env.SOME_SECRET);
exports.func1 = function() {
console.log(process.env.SOME_SECRET);
}
In this case, the result would be like this:
undefined
SECRET
The reason is that at the moment when the interpreter starts and interprets each line, the global env variable is undefined
, and after the dotenv
module is interpreted and processed, the environment variables sit in RAM and get values.
Therefore, in the last line where func1
is executed, the environment variable has value.
That's why it is recommended to import dotenv
at the very first line in the main file.
So the correct way would be like this:
// main.js
require('dotenv').config();
const { func1 } = require('./utils.js');
func1();
CodePudding user response:
Thank you for your help!
The answers were right that I dad to import dotenv
into the controller file that I wanted.
But I had to do it in the format
const dotenv = require('dotenv');
dotenv.config({path: './config/.env'})
Once I added the path it worked no problem!