Created
October 16, 2017 19:55
-
-
Save david-mart/e85c8ed644ecaa123e3a0649ff441119 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ({ body }, response) => { | |
const { id_token } = body; | |
checkUserToken(id_token).then(({ _payload }) => { | |
const { name, email, picture } = _payload; | |
User.find({ | |
where: { | |
} | |
}).then(user => { | |
if (!user) { | |
User.create({ name, email, picture }).then(() => { | |
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