mirror of
https://gitlab.com/little-lines/backend.git
synced 2024-11-25 08:06:51 +00:00
Merge branch 'main' of https://gitlab.com/openKMITL/little-lines/backend
This commit is contained in:
commit
3ff828d9c9
4 changed files with 135 additions and 2 deletions
|
@ -1,7 +1,12 @@
|
||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
const sendToken = require('../utils/jwtToken');
|
const sendToken = require('../utils/jwtToken');
|
||||||
|
|
||||||
const user = require('../models/user');
|
const user = require('../models/user');
|
||||||
|
|
||||||
|
const axios = require('axios');
|
||||||
|
const dotenv = require("dotenv").config();
|
||||||
|
|
||||||
|
|
||||||
// Get all users => GET api/users
|
// Get all users => GET api/users
|
||||||
const getAllUser = asyncHandler(async (req, res) => {
|
const getAllUser = asyncHandler(async (req, res) => {
|
||||||
const users = await user.find();
|
const users = await user.find();
|
||||||
|
@ -109,4 +114,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",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^1.6.2",
|
||||||
"bcryptjs": "^2.4.3",
|
"bcryptjs": "^2.4.3",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"cookie-session": "^2.0.0",
|
"cookie-session": "^2.0.0",
|
||||||
|
@ -89,6 +90,21 @@
|
||||||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||||
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
|
"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": {
|
"node_modules/balanced-match": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
||||||
|
@ -209,6 +225,17 @@
|
||||||
"fsevents": "~2.3.2"
|
"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": {
|
"node_modules/concat-map": {
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||||
|
@ -305,6 +332,14 @@
|
||||||
"ms": "2.0.0"
|
"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": {
|
"node_modules/depd": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
|
||||||
|
@ -504,6 +539,38 @@
|
||||||
"node": ">= 0.8"
|
"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": {
|
"node_modules/forwarded": {
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
|
||||||
|
@ -1130,6 +1197,11 @@
|
||||||
"node": ">= 0.10"
|
"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": {
|
"node_modules/pstree.remy": {
|
||||||
"version": "1.1.8",
|
"version": "1.1.8",
|
||||||
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
|
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^1.6.2",
|
||||||
"bcryptjs": "^2.4.3",
|
"bcryptjs": "^2.4.3",
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"cookie-session": "^2.0.0",
|
"cookie-session": "^2.0.0",
|
||||||
|
|
|
@ -5,7 +5,8 @@ const { getAllUser,
|
||||||
deleteUser,
|
deleteUser,
|
||||||
updateUser,
|
updateUser,
|
||||||
loginUser,
|
loginUser,
|
||||||
logoutUser} = require('../controllers/userController');
|
logoutUser,
|
||||||
|
googleAuth} = require('../controllers/userController');
|
||||||
|
|
||||||
router.route('/').get(getAllUser);
|
router.route('/').get(getAllUser);
|
||||||
router.route('/create').post(createUser);
|
router.route('/create').post(createUser);
|
||||||
|
@ -14,4 +15,6 @@ router.route('/update/:id').put(updateUser);
|
||||||
router.route('/login').post(loginUser);
|
router.route('/login').post(loginUser);
|
||||||
router.route('/logout').get(logoutUser);
|
router.route('/logout').get(logoutUser);
|
||||||
|
|
||||||
|
router.route('/googleAuth').post(googleAuth);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
Loading…
Reference in a new issue