Skip to content

Commit

Permalink
[Server] / #16 / add google oAuth2.0
Browse files Browse the repository at this point in the history
[Server] / #16 / add google oAuth2.0
  • Loading branch information
Dev-SeungcheolLee authored Nov 16, 2020
2 parents fd79792 + 16fdeda commit 0c87345
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 74 deletions.
78 changes: 78 additions & 0 deletions controller/users/google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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');

console.log(sub);

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);
}
};

3 changes: 2 additions & 1 deletion controller/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
signin: require('./signin'),
signout: require('./signout'),
signup: require('./signup'),
remove: require('./remove')
remove: require('./remove'),
google: require('./google')
};
69 changes: 0 additions & 69 deletions controller/users/signin.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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);
}
};
10 changes: 6 additions & 4 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 0c87345

Please sign in to comment.