mirror of
https://gitlab.com/little-lines/backend.git
synced 2024-11-21 17:36:51 +00:00
add loginGoogleAuth
This commit is contained in:
parent
e534935d80
commit
b026a81473
4 changed files with 132 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
|||
const asyncHandler = require('express-async-handler');
|
||||
const user = require('../models/user');
|
||||
const sendToken = require('../utils/jwtToken');
|
||||
const axios = require('axios');
|
||||
const dotenv = require("dotenv").config();
|
||||
|
||||
// Get all users => GET api/users
|
||||
const getAllUser = asyncHandler(async (req, res) => {
|
||||
|
@ -109,4 +111,56 @@ const logoutUser = asyncHandler(async (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
module.exports = { getAllUser, createUser, deleteUser, updateUser, loginUser, logoutUser };
|
||||
const googleAuth = asyncHandler(async (req, res) => {
|
||||
try {
|
||||
// get the code from frontend
|
||||
const code = req.headers.authorization;
|
||||
console.log('Authorization Code:', code);
|
||||
|
||||
// Exchange the authorization code for an access token
|
||||
const response = await axios.post(
|
||||
'https://oauth2.googleapis.com/token',
|
||||
{
|
||||
code,
|
||||
client_id: process.env.CLIENT_ID,
|
||||
client_secret: process.env.CLIENT_SECRET,
|
||||
redirect_uri: 'postmessage',
|
||||
grant_type: 'authorization_code'
|
||||
}
|
||||
);
|
||||
const accessToken = response.data.access_token;
|
||||
console.log('Access Token:', accessToken);
|
||||
|
||||
// Fetch user details using the access token
|
||||
const userResponse = await axios.get(
|
||||
'https://www.googleapis.com/oauth2/v3/userinfo',
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
const userDetails = userResponse.data;
|
||||
console.log('User Details:', userDetails);
|
||||
|
||||
// Process user details and perform necessary actions
|
||||
|
||||
const users = await user.findOne({ email: userDetails.email })
|
||||
|
||||
if(!users){
|
||||
const newUser = await user.create({
|
||||
username: userDetails.name,
|
||||
password:"nothing",
|
||||
email: userDetails.email,
|
||||
isGoogleAccount: userDetails.email_verified
|
||||
});
|
||||
}
|
||||
|
||||
sendToken(users, 200, res);
|
||||
} catch (error) {
|
||||
console.error('Error saving code:', error);
|
||||
res.status(500).json({ message: 'Failed to save code' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = { getAllUser, createUser, deleteUser, updateUser, loginUser, logoutUser , googleAuth};
|
72
package-lock.json
generated
72
package-lock.json
generated
|
@ -9,6 +9,7 @@
|
|||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^1.6.2",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"body-parser": "^1.20.2",
|
||||
"cookie-session": "^2.0.0",
|
||||
|
@ -85,6 +86,21 @@
|
|||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
|
||||
},
|
||||
"node_modules/asynckit": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "1.6.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz",
|
||||
"integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.15.0",
|
||||
"form-data": "^4.0.0",
|
||||
"proxy-from-env": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/balanced-match": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
||||
|
@ -205,6 +221,17 @@
|
|||
"fsevents": "~2.3.2"
|
||||
}
|
||||
},
|
||||
"node_modules/combined-stream": {
|
||||
"version": "1.0.8",
|
||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||
"dependencies": {
|
||||
"delayed-stream": "~1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
|
@ -301,6 +328,14 @@
|
|||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/delayed-stream": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/depd": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
|
||||
|
@ -500,6 +535,38 @@
|
|||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/follow-redirects": {
|
||||
"version": "1.15.3",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz",
|
||||
"integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||
}
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"debug": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/form-data": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
|
||||
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
|
||||
"dependencies": {
|
||||
"asynckit": "^0.4.0",
|
||||
"combined-stream": "^1.0.8",
|
||||
"mime-types": "^2.1.12"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/forwarded": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
|
||||
|
@ -1126,6 +1193,11 @@
|
|||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-from-env": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||
},
|
||||
"node_modules/pstree.remy": {
|
||||
"version": "1.1.8",
|
||||
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
|
||||
|
|
|
@ -6,12 +6,12 @@
|
|||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "nodemon server.js"
|
||||
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^1.6.2",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"body-parser": "^1.20.2",
|
||||
"cookie-session": "^2.0.0",
|
||||
|
|
|
@ -5,7 +5,8 @@ const { getAllUser,
|
|||
deleteUser,
|
||||
updateUser,
|
||||
loginUser,
|
||||
logoutUser} = require('../controllers/userController');
|
||||
logoutUser,
|
||||
googleAuth} = require('../controllers/userController');
|
||||
|
||||
router.route('/').get(getAllUser);
|
||||
router.route('/create').post(createUser);
|
||||
|
@ -14,4 +15,6 @@ router.route('/update/:id').put(updateUser);
|
|||
router.route('/login').post(loginUser);
|
||||
router.route('/logout').get(logoutUser);
|
||||
|
||||
router.route('/googleAuth').post(googleAuth);
|
||||
|
||||
module.exports = router;
|
Loading…
Reference in a new issue