1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-10 09:02:49 +00:00

fetch client id from server for oauth

This commit is contained in:
2018-04-15 16:16:24 -05:00
parent f13b177a93
commit 5d5717effe
5 changed files with 60 additions and 15 deletions

View File

@@ -1,18 +1,15 @@
import React from 'react'; import React from 'react';
import { NavLink } from 'react-router-dom'; import { NavLink } from 'react-router-dom';
import jwt_decode from 'jwt-decode'; import jwt_decode from 'jwt-decode';
import { StorageService } from '../../services'; import { OauthService, StorageService } from '../../services';
import './Navbar.scss'; import './Navbar.scss';
const baseUrl = window.location.origin + '/oauth';
const oauthUrl = `https://discordapp.com/api/oauth2/authorize?client_id=410818759746650140&redirect_uri=${baseUrl}&response_type=code&scope=identify%20guilds`;
interface Props {} interface Props {}
interface State { interface State {
token: string | null; token: string | null;
email?: string; email?: string;
oauthUrl?: string;
} }
export class Navbar extends React.Component<Props, State> { export class Navbar extends React.Component<Props, State> {
@@ -24,20 +21,46 @@ export class Navbar extends React.Component<Props, State> {
} }
componentDidMount() { componentDidMount() {
this.loadOauthUrl();
const token = StorageService.getJWT(); const token = StorageService.getJWT();
if (token) { if (token) {
const claims: any = jwt_decode(token!); const claims: any = jwt_decode(token);
const email = claims['email']; const email = claims['email'];
this.setState({ token, email }); this.setState({ token, email });
} }
} }
async loadOauthUrl() {
try {
const oauthUrl = await OauthService.getOauthUrl();
this.setState({ oauthUrl });
} catch (e) {
console.error(e);
}
}
private logout = () => { private logout = () => {
StorageService.clear(); StorageService.clear();
window.location.href = '/'; window.location.href = '/';
}; };
renderLoginButton() {
if (!this.state.oauthUrl) {
return null;
}
return !this.state.token ? (
<a href={this.state.oauthUrl} className="Navbar__item">
Login
</a>
) : (
<a className="Navbar__item" onClick={this.logout}>
Logout
</a>
);
}
render() { render() {
return ( return (
<div className="Navbar"> <div className="Navbar">
@@ -58,15 +81,7 @@ export class Navbar extends React.Component<Props, State> {
Stats Stats
</NavLink> </NavLink>
{!this.state.token ? ( {this.renderLoginButton()}
<a href={oauthUrl} className="Navbar__item">
Login
</a>
) : (
<a className="Navbar__item" onClick={this.logout}>
Logout
</a>
)}
{this.state.email && <div className="Navbar__email">{this.state.email}</div>} {this.state.email && <div className="Navbar__email">{this.state.email}</div>}
</div> </div>

View File

@@ -1,2 +1,3 @@
export * from './axios.service'; export * from './axios.service';
export * from './oauth.service';
export * from './storage.service'; export * from './storage.service';

View File

@@ -0,0 +1,18 @@
import { axios } from './axios.service';
const redirectUrl = window.location.origin + '/oauth';
const getClientID = async (): Promise<string> => {
const res = await axios.get('/api/config/client_id');
return res.data['id'];
};
const getOauthUrl = async (): Promise<string> => {
const clientID = await getClientID();
return `https://discordapp.com/api/oauth2/authorize?client_id=${clientID}&redirect_uri=${redirectUrl}&response_type=code&scope=email%20identify`;
};
export const OauthService = {
getClientID,
getOauthUrl,
};

View File

@@ -0,0 +1,10 @@
package handlers
import (
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/config"
)
func GetClientID(c *gin.Context) {
c.JSON(200, map[string]string{"id": config.Config.ClientID})
}

View File

@@ -30,6 +30,7 @@ func getRouter() *gin.Engine {
api.POST("/oauth", handlers.Oauth) api.POST("/oauth", handlers.Oauth)
api.GET("/logger/messages", handlers.GetMessages) api.GET("/logger/messages", handlers.GetMessages)
api.GET("/logger/linkedmessages", handlers.GetLinkedMessages) api.GET("/logger/linkedmessages", handlers.GetLinkedMessages)
api.GET("/config/client_id", handlers.GetClientID)
authorizedAPI := router.Group("/api") authorizedAPI := router.Group("/api")
authorizedAPI.Use(middleware.AuthorizedJWT()) authorizedAPI.Use(middleware.AuthorizedJWT())