-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b775d65
commit 762bf52
Showing
16 changed files
with
292 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Request, Response } from 'express'; | ||
import { initiateGoogleLogin } from '../controllers/google.auth'; | ||
|
||
describe('initiateGoogleLogin', () => { | ||
const req = {} as Request; | ||
const res = { | ||
redirect: jest.fn(), | ||
} as unknown as Response; | ||
const next = jest.fn(); | ||
|
||
it('should redirect to Google OAuth with appropriate scope', () => { | ||
initiateGoogleLogin(req, res, next); | ||
|
||
expect(res.redirect).toHaveBeenCalledWith( | ||
expect.stringContaining('google') | ||
); | ||
expect(res.redirect).toHaveBeenCalledWith( | ||
expect.stringContaining('profile') | ||
); | ||
expect(res.redirect).toHaveBeenCalledWith( | ||
expect.stringContaining('email') | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const dotenv = require('dotenv'); | ||
|
||
dotenv.config(); | ||
|
||
const getPrefix = () => { | ||
const env = process.env.NODE_ENV || 'production'; | ||
const envPrefixMap = { | ||
development: 'DATABASE', | ||
test: 'TEST_DATABASE', | ||
production: 'PROD_DATABASE', | ||
}; | ||
const prefix = envPrefixMap[env]; | ||
return prefix; | ||
}; | ||
|
||
const getDatabaseConfig = () => { | ||
const prefix = getPrefix(); | ||
const config = { | ||
secret: process.env.JWT_SECRET || 'secret', | ||
}; | ||
|
||
if (prefix === 'PROD_DATABASE') { | ||
config.dialectOptions = { | ||
ssl: { | ||
require: true, | ||
rejectUnauthorized: false, | ||
}, | ||
}; | ||
} | ||
return config; | ||
}; | ||
|
||
module.exports = getDatabaseConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable no-console */ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import passport from 'passport'; | ||
import { User } from '../entities/User'; | ||
import { responseSuccess, signToken } from '../utils'; | ||
|
||
export const initiateGoogleLogin = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
passport.authenticate('google', { scope: ['profile', 'email'] })( | ||
req, | ||
res, | ||
next | ||
); | ||
}; | ||
|
||
export const handleGoogleCallback = async (req: Request, res: Response) => { | ||
passport.authenticate( | ||
'google', | ||
async (err: unknown, user: User | null) => { | ||
if (err) { | ||
return res.status(500).json({error: 'Failed to authenticate with Google'}); | ||
} | ||
if (!user) { | ||
return res.status(401).json({error: 'User not found'}); | ||
} | ||
|
||
try { | ||
const token = signToken({ id: user.id }); | ||
|
||
responseSuccess(res, 200, token, 'User authenticated successfully'); | ||
} catch (error) { | ||
console.error('Error occurred:', error); | ||
res.status(500).json({ error: 'Internal server error' }); | ||
} | ||
|
||
} | ||
)(req, res); | ||
}; | ||
|
||
export { passport }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Router } from 'express'; | ||
import * as authController from '../controllers/google.auth'; | ||
|
||
const routerGmail = Router(); | ||
|
||
routerGmail.get('/users/google-auth', authController.initiateGoogleLogin); | ||
routerGmail.get('/users/google-auth/callback', authController.handleGoogleCallback); | ||
|
||
export default routerGmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { | ||
v2 as cloudinary, | ||
UploadApiResponse, | ||
UploadApiErrorResponse, | ||
} from 'cloudinary'; | ||
import dotenv from 'dotenv'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
|
||
dotenv.config(); | ||
|
||
cloudinary.config({ | ||
cloud_name: process.env.CLOUDINARY_NAME, | ||
api_key: process.env.CLOUDINARY_KEY, | ||
api_secret: process.env.CLOUDINARY_SECRET, | ||
}); | ||
|
||
export const cloudUpload = async ( | ||
buffer: Buffer, | ||
fileName: string | ||
): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
const tempFilePath = path.join(os.tmpdir(), fileName); | ||
|
||
fs.writeFile(tempFilePath, buffer, writeError => { | ||
if (writeError) { | ||
reject(writeError); | ||
return; | ||
} | ||
|
||
cloudinary.uploader.upload( | ||
tempFilePath, | ||
( | ||
uploadError: UploadApiErrorResponse | undefined, | ||
result: UploadApiResponse | ||
) => { | ||
|
||
if (uploadError) { | ||
reject(new Error(uploadError.message)); | ||
} else { | ||
const uploadResult: string = result.secure_url; | ||
resolve(uploadResult); | ||
} | ||
} | ||
); | ||
}); | ||
}); | ||
}; | ||
export const deleteCloudinaryFile = async (url: string) => { | ||
try { | ||
await cloudinary.uploader.destroy(url); | ||
return true; | ||
} catch (error) { | ||
return error; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
// export all utils | ||
export * from './response.utils'; | ||
export * from './logger'; | ||
export * from './cloud'; | ||
export * from './jwtToken'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import getDatabaseConfig from '../configs/config'; | ||
|
||
|
||
const { secret } = getDatabaseConfig(); | ||
|
||
// Verifies a token | ||
export const verifyToken = ( | ||
token: string | ||
): { email: string; id: string } | null => { | ||
const decoded = jwt.verify(token, secret) as { email: string; id: string }; | ||
return decoded; | ||
}; | ||
|
||
// Generates a token | ||
interface TokenPayload { | ||
id?: string; | ||
email?: string; | ||
} | ||
|
||
export const signToken = ( | ||
payload: TokenPayload, | ||
expiresIn: string = '2h' | ||
): string => { | ||
const token = jwt.sign(payload, secret, { expiresIn }); | ||
return token; | ||
}; |
Oops, something went wrong.