Skip to content

Instantly share code, notes, and snippets.

@oshalygin
Created October 16, 2017 19:59
Show Gist options
  • Save oshalygin/08d7f54e838e6ad0821711ce84cd7bda to your computer and use it in GitHub Desktop.
Save oshalygin/08d7f54e838e6ad0821711ce84cd7bda to your computer and use it in GitHub Desktop.
import GoogleAuth from 'google-auth-library';
import { googleAuthClientId } from '../../src/constants/config';
import { User } from '../utilities/database';
import { path } from 'ramda';
const checkUserToken = token => {
return new Promise((resolve, reject) => {
const auth = new GoogleAuth();
const client = new auth.OAuth2(googleAuthClientId, '', '');
client.verifyIdToken(token, googleAuthClientId, (error, login) => {
if (error) {
reject(error);
}
resolve(login);
});
});
};
const authenticateUser = async ({ body }, response) => {
const { id_token } = body;
const { _payload } = await checkUserToken(id_token);
const { name, email, picture } = _payload;
const user = await User.find({
where: {
email,
},
});
if (!user) {
await User.create({ name, email, picture });
return response.status(401).json({ status: 'Not Authorized' });
}
if (path('permissions', user)) {
return response.status(200).json(user);
}
return response.status(401).json({ status: 'Not Authorized' });
};
const authController = {
post: authenticateUser,
};
export default authController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment