desc time author veaba
 *  import { getKeysDB , insertOne , isHasOne , updateOne } from "../mongo/curd" ; import 'url-search-params-polyfill' ; const { Router } = require ( 'express' ) ; const router = Router ( ) ; import { githubOAuthConfig } from '../config' ; import { axios } from "../utils/axios" ; import { setHash } from "../redis/redis" ; import { _pushPrivate , _pushSuccess } from "../app" ; import { _authFail , _authSuccess } from "../utils/utils" ; const handlerRedirect = async ( req , res ) => { const { query } = req ; const { code } = query || { } ; if ( ! code ) { res . set ( 'Content-Type' , 'text/html' ) ; res . send ( _authFail ( ) ) ; } const githubToken = await axios . post ( githubOAuthConfig . getTokenUrl , { client_id : githubOAuthConfig . client_id , client_secret : githubOAuthConfig . client_secret , code , } ) ; const allUrl = new URLSearchParams ( githubToken ) ; const access_token = allUrl . get ( 'access_token' ) ; const sid = req . params . sid ; if ( ! sid ) { return res . send ( _authFail ( ) ) ; } axios . get ( githubOAuthConfig . getApiUrl , { headers : { 'Authorization' : 'token ' + access_token } } ) . then ( async ( apiRes ) => { if ( apiRes . id ) { const reqRedisObj = { isAdmin : false } ; if ( apiRes . login === 'veaba' ) { reqRedisObj . isAdmin = true ; } if ( await isHasOne ( { name : apiRes . login } , 'users' ) ) { await updateOne ( { name : apiRes . login } , { githubOAuthObj : apiRes , } , 'users' ) ; const { _id } = await getKeysDB ( { name : apiRes . login } , [ ] , 'users' ) ; reqRedisObj . _id = _id . toString ( ) ; reqRedisObj . avatar_url = apiRes . avatar_url ; reqRedisObj . name = apiRes . login ; reqRedisObj . sid = sid ; } else { const { _id , name , githubOAuthObj } = await insertOne ( { name : apiRes . login , githubOAuthObj : apiRes , ... reqRedisObj } , 'users' ) ; reqRedisObj . avatar_url = githubOAuthObj . avatar_url ; reqRedisObj . _id = _id . toString ( ) ; reqRedisObj . name = name ; reqRedisObj . sid = sid ; } await setHash ( sid , reqRedisObj ) ; await _pushPrivate ( '/broadcast' , 'auth' , '/broadcast#' + sid , [ ] , '已授权访问Github' , 0 ) ; res . set ( 'Content-Type' , 'text/html' ) ; res . send ( _authSuccess ( ) ) ; } } ) ; } ; router . get ( '/redirect/github/:sid' , handlerRedirect ) ; export { router } ;