Home >Web Front-end >JS Tutorial >A brief analysis of how node implements github third-party login
nodeHow to implement github third-party login? The following article will introduce to you how to implement third-party login to github using nodejs. I hope it will be helpful to you!
①Log in to github, Settings=>Developer settings=>OAuth Apps=>Register a new application
② Fill in the application information
③ Registration is completed and get Client ID and Client Secret
[Related tutorial recommendations: nodejs video tutorial]
<a href="https://github.com/login/oauth/authorize?client_id={你自己的cilent_id}&redirect_uri=http://localhost:3001/github" class="iconfont ali-icon-github"></a>
router.get('/github',controller.auth.githubLogin)
const axios = require('axios') const querystring = require('querystring') const config = { client_id: "你自己的client_id", client_secret: "你自己的client_secret" } class AuthController { async githubLogin(ctx) { const code = ctx.request.query.code const params = { client_id: config.client_id, client_secret: config.client_secret, code: code } let res = await axios.post('https://github.com/login/oauth/access_token', params) console.log(res) const token = querystring.parse(res.data).access_token ctx.cookies.set('token', token, { maxAge: ctx.config.jwt.expire * 1000, }); res = { ...ctx.errCode.SUCCESS, data: { token } }; ctx.redirect('http://172.25.78.33:8081/login/success?token='+token) } } module.exports = exports = new AuthController();
The temporary page will jump very quickly and is basically invisible.
<template> <h1>登录成功跳转首页</h1> </template> <script> import {setLoginedUser} from "@/http/axios"; export default { mounted() { setLoginedUser("github", this.$route.query.token); this.$message({ message: "登录成功", type: "success", }); this.$router.push("/home"); }, }; </script> <style> </style>
https://github.com/wantao666/nodejs-github
For more node-related knowledge, please visit : nodejs tutorial!
The above is the detailed content of A brief analysis of how node implements github third-party login. For more information, please follow other related articles on the PHP Chinese website!