From 0876fa78760a031634c2d9e437e7a001f5990a62 Mon Sep 17 00:00:00 2001 From: seungcheol Date: Mon, 16 Nov 2020 09:55:35 +0900 Subject: [PATCH 1/2] feature#16:add google oAuth signin and edit user model --- controller/users/google.js | 76 ++++++++++++++++++++++++++++++++++++++ controller/users/index.js | 3 +- controller/users/signin.js | 69 ---------------------------------- models/user.js | 10 +++-- routes/users.js | 1 + 5 files changed, 85 insertions(+), 74 deletions(-) create mode 100644 controller/users/google.js diff --git a/controller/users/google.js b/controller/users/google.js new file mode 100644 index 0000000..3840773 --- /dev/null +++ b/controller/users/google.js @@ -0,0 +1,76 @@ +require('dotenv').config(); +const { user } = require('../../models'); +const { OAuth2Client } = require('google-auth-library'); +const client = new OAuth2Client(process.env.googleClientId); +const jwt = require('jsonwebtoken'); + +module.exports = { + post: (req, res) => { + const updateToken = payload => { + const { sub, name, email } = payload; + const token = jwt.sign({ + id: sub, + name, + email + }, 'testSecret'); + + user.update({ + token: token + }, { + where: { + googleId: sub + } + }).catch(err => console.log(err)); + + return token; + }; + + const insertUserIntoDB = payload => { + const { sub, name, email } = payload; + const token = jwt.sign({ + id: sub, + name, + email + }, 'testSecret'); + + user.create({ + googleId: sub, + email: email, + username: name, + token: token + }).catch(err => console.log(err)); + + return token; + }; + + async function verify() { + const ticket = await client.verifyIdToken({ + idToken: req.body.tokenId + }); + const payload = ticket.getPayload(); + const googleId = payload['sub']; + + user.findOne({ + attributes: ['token'] + }, { + where: { + googleId: googleId + } + }).then(result => { + let token = ''; + console.log(result); + if (result.length > 0) { + console.log('DB에 있는 유저'); + token = updateToken(payload); + } else { + console.log('DB에 없는 유저'); + token = insertUserIntoDB(payload); + } + res.send({ token }); + }).catch(err => console.log(err)); + }; + + verify().then(() => { }).catch(console.error); + } +}; + diff --git a/controller/users/index.js b/controller/users/index.js index 30fb200..8bab438 100644 --- a/controller/users/index.js +++ b/controller/users/index.js @@ -5,5 +5,6 @@ module.exports = { signin: require('./signin'), signout: require('./signout'), signup: require('./signup'), - remove: require('./remove') + remove: require('./remove'), + google: require('./google') }; \ No newline at end of file diff --git a/controller/users/signin.js b/controller/users/signin.js index 8df2c82..fcba2c1 100644 --- a/controller/users/signin.js +++ b/controller/users/signin.js @@ -1,8 +1,4 @@ -require('dotenv').config(); const { user } = require('../../models'); -const { OAuth2Client } = require('google-auth-library'); -const client = new OAuth2Client(process.env.googleClientId); -const jwt = require('jsonwebtoken'); module.exports = { post: (req, res) => { @@ -24,70 +20,5 @@ module.exports = { }).catch(err => res.status(500).send(err)); } - const updateToken = payload => { - const { sub, name, email } = payload; - const token = jwt.sign({ - id: sub, - name, - email - }, 'testSecret'); - - user.update({ - token: token - }, { - where: { - googleId: sub - } - }).catch(err => console.log(err)); - - return token; - }; - - const insertUserIntoDB = payload => { - const { sub, name, email } = payload; - const token = jwt.sign({ - id: sub, - name, - email - }, 'testSecret'); - - user.create({ - googleId: sub, - email: email, - username: name, - token: token - }).catch(err => console.log(err)); - - return token; - }; - - async function verify() { - const ticket = await client.verifyIdToken({ - idToken: req.body.it - }); - const payload = ticket.getPayload(); - const googleId = payload['sub']; - - user.findAll({ - attributes: ['token'] - }, { - where: { - googleId: googleId - } - }).then(result => { - let token = ''; - console.log(result); - if (result.length > 0) { - console.log('DB에 있는 유저'); - token = updateToken(payload); - } else { - console.log('DB에 없는 유저'); - token = insertUserIntoDB(payload); - } - res.send({ token }); - }).catch(err => console.log(err)); - }; - - verify().then(() => { }).catch(console.error); } }; \ No newline at end of file diff --git a/models/user.js b/models/user.js index 1733288..ceb4189 100644 --- a/models/user.js +++ b/models/user.js @@ -22,12 +22,14 @@ module.exports = (sequelize, DataTypes) => { }, { hooks: { beforeCreate: data => { - let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$'); - shasum.update(data.password); - data.password = shasum.digest('hex'); + if (data.password) { + let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$'); + shasum.update(data.password); + data.password = shasum.digest('hex'); + } }, beforeFind: data => { - if (data.where.password) { + if (Object.keys(data).indexOf('where') !== -1) { let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$'); shasum.update(data.where.password); data.where.password = shasum.digest('hex'); diff --git a/routes/users.js b/routes/users.js index e3f18df..d102a14 100644 --- a/routes/users.js +++ b/routes/users.js @@ -10,5 +10,6 @@ router.get('/info', usersController.info.get); router.patch('/editinfo', usersController.editinfo.patch); router.post('/checkpwd', usersController.checkpwd.post); router.post('/remove', usersController.remove.post); +router.post('/google', usersController.google.post); module.exports = router; \ No newline at end of file From 16fdeda9c06a2a8832c349205452b1b917217d79 Mon Sep 17 00:00:00 2001 From: seungcheol Date: Mon, 16 Nov 2020 11:03:53 +0900 Subject: [PATCH 2/2] feature#16 --- controller/users/google.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controller/users/google.js b/controller/users/google.js index 3840773..fe68d6f 100644 --- a/controller/users/google.js +++ b/controller/users/google.js @@ -33,6 +33,8 @@ module.exports = { email }, 'testSecret'); + console.log(sub); + user.create({ googleId: sub, email: email,