Home >Web Front-end >JS Tutorial >A brief analysis of how node implements github third-party login

A brief analysis of how node implements github third-party login

青灯夜游
青灯夜游forward
2022-10-26 19:23:171835browse

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!

A brief analysis of how node implements github third-party login

1. Detailed process

A brief analysis of how node implements github third-party login

2. Detailed process

1. Register application

①Log in to github, Settings=>Developer settings=>OAuth Apps=>Register a new application

A brief analysis of how node implements github third-party login

A brief analysis of how node implements github third-party login
② Fill in the application information
A brief analysis of how node implements github third-party login

③ Registration is completed and get Client ID and Client Secret

A brief analysis of how node implements github third-party login

[Related tutorial recommendations: nodejs video tutorial]

2. The front-end initiates a request to the github authorization page. If the authorization is successful, the code is redirected to the configured back-end callback URL

<a href="https://github.com/login/oauth/authorize?client_id={你自己的cilent_id}&redirect_uri=http://localhost:3001/github" class="iconfont ali-icon-github"></a>

3. The back-end gets the code, requests github with the code, and gets to the token, and then put the token on the url and pass it to the front end

router.get(&#39;/github&#39;,controller.auth.githubLogin)
const axios = require(&#39;axios&#39;)
const querystring = require(&#39;querystring&#39;)

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(&#39;https://github.com/login/oauth/access_token&#39;, params)
        console.log(res)
        const token = querystring.parse(res.data).access_token
        ctx.cookies.set(&#39;token&#39;, token, {
            maxAge: ctx.config.jwt.expire * 1000,
        });
        res = { ...ctx.errCode.SUCCESS, data: { token } };
        ctx.redirect(&#39;http://172.25.78.33:8081/login/success?token=&#39;+token)
    }
}
module.exports = exports = new AuthController();

4. The front end creates a temporary page, saves the token on the url, and jumps to the login success page

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>

3. Code link

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete