Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #72 from ovsoinc/plus
Browse files Browse the repository at this point in the history
Plus
  • Loading branch information
montyanderson committed Feb 23, 2020
2 parents 7518a86 + bf4ad5d commit 05e963f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
27 changes: 27 additions & 0 deletions lib/oauth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const axios = require('axios');

module.exports = {
async getAccessToken(code) {
const {data:{access_token}} = await axios.post('https://github.com/login/oauth/access_token', {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code
}, {
headers: {
Accept: 'application/json'
}
});

return access_token;
},

async getUser(accessToken) {
const {data} = await axios.get('https://api.github.com/user', {
headers: {
Authorization: `token ${accessToken}`
}
});

return data;
}
};
16 changes: 16 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const search = require('./search');
const rclone = require('./lib/rclone');
const pathing = require('./lib/pathing');
const speedStats = require('./lib/speed-stats');
const oauth = require('./lib/oauth');

const app = module.exports = new Koa();
const router = new Router();
Expand Down Expand Up @@ -178,6 +179,21 @@ router.get('/worker-stats', async ctx => {
}))), null, "\t")
});

router.post('/login', async ctx => {
const {code} = ctx.query;

const accessToken = await oauth.getAccessToken(code);
const user = await oauth.getUser(accessToken);

console.log(user);

await redis.zadd('plus-users', 'NX', Date.now(), user.login);

ctx.body = JSON.stringify({
user
});
});

router.use('/lock', async (ctx, next) => {
const token = ctx.request.headers['x-worker-token'];

Expand Down
38 changes: 35 additions & 3 deletions ui/components/GithubLogin.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div class="login">
<a v-bind:href="authorizeUrl" class="btn btn-large btn-success"><i class="fab fa-github"></i> Login with GitHub</a>
<div v-if="loginEnabled" class="login">
<a v-if="user === undefined" v-bind:href="authorizeUrl" v-bind:disabled="loading" class="btn btn-large btn-success"><i class="fab fa-github"></i> Login with GitHub</a>
<a v-else class="btn btn-large btn-primary white-text"><i class="fab fa-github"></i> {{user.login}}</a>
</div>
</template>

Expand All @@ -15,15 +16,46 @@
</style>

<script>
const axios = require('axios');
module.exports = {
data: () => ({
loginEnabled: false,
clientId: 'd907d5253811062b6d1f',
redirectUri: window.location.href
redirectUri: window.location.href.split('?')[0],
loading: false,
user: undefined
}),
computed: {
authorizeUrl() {
return `https://github.com/login/oauth/authorize?client_id=${this.clientId}&redirect_uri=${this.redirectUri}`;
}
},
async created() {
const urlParams = new URLSearchParams(window.location.search);
if(urlParams.has('login-enabled') === true) {
localStorage.setItem('login-enabled', 'true');
}
if(typeof localStorage.getItem('login-enabled') === 'string') {
this.loginEnabled = true;
}
const code = urlParams.get('code');
if(typeof code === 'string' && code.length > 0) {
this.loading = true;
const { data: { user } } = await axios.post('/login', undefined, {
params: {
code
}
});
this.loading = false;
this.user = user;
}
}
};
</script>

0 comments on commit 05e963f

Please sign in to comment.