Skip to content

Commit

Permalink
need to store access token
Browse files Browse the repository at this point in the history
  • Loading branch information
yongli-abc committed Jan 7, 2018
1 parent 7c5f1a8 commit 34db9d2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
22 changes: 12 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const {app, BrowserWindow, Menu, Tray, ipcMain} = require("electron");
const path = require("path");
const url = require("url");
const config = require("./config.js");
const config = require("./config");
const util = require("./src/js/util");

/*
* Start in development mode
Expand Down Expand Up @@ -67,17 +68,18 @@ function setMessageListener() {
authWindow.webContents.on("did-get-redirect-request", function(event, oldUrl, newUrl) {
console.log("get redirect request, event=", event, "oldUrl=", oldUrl, "newUrl=", newUrl);

var error = RegExp('[?&]error=([^&]*)').exec(newUrl);
var token = RegExp('[?&#]token=([^&]*)').exec(newUrl);
var expires_in = RegExp('[?&]expires_in=([^&]*)').exec(newUrl);
var errorMatch = RegExp('[?&]error=([^&]*)').exec(newUrl);
var tokenMatch = RegExp('[?&#]access_token=([^&]*)').exec(newUrl);
var expiresMatch = RegExp('[?&]expires_in=([^&]*)').exec(newUrl);

if (error) {
indexSender.send("login-error", error);
if (errorMatch) {
indexSender.send("login-error", errorMatch[1]);
closeAuthWindow();
} else if (tokenMatch && expiresMatch) {
var expired_at = new Date((new Date()).getTime() + parseInt(expiresMatch[1]) * 1000);
util.user.setToken(tokenMatch[1], expired_at);
indexSender.send("login-success");
closeAuthWindow();
} else if (token && expiresIn) {
var expired_at = new Date((new Date()).getTime() + expires_in * 1000);
localStorage.access_token = token;
localStorage.expired_at = expired_at;
}
});

Expand Down
6 changes: 5 additions & 1 deletion src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ util.loadVueP()
ipcRenderer.on("login-error", (event, arg) => {
alert("η™»ε½•ζŽˆζƒε€±θ΄₯οΌŒθ―·ι‡θ―•γ€‚");
});

ipcRenderer.on("login-success", () => {
alert("η™»ε½•ζˆεŠŸ, token=", localStorage.access_token, ", expired_at=", localStorage.expired_at);
});
})
.then(function() { // start vue app
app = new Vue({
Expand All @@ -35,7 +39,7 @@ util.loadVueP()
view: k_view.init,
word: null,
error: null,
hasLogin: util.hasLogin()
hasLogin: util.user.tokenValid()
},
methods: {
onFocus: function() {
Expand Down
4 changes: 2 additions & 2 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ module.exports = {
return localStorage.access_token;
},
setToken: function(access_token, expired_at) {
localStorage.access_token = access_token;
localStorage.expired_at = tokenExpired;
window.localStorage.access_token = access_token;
window.localStorage.expired_at = tokenExpired;
}
}
};

0 comments on commit 34db9d2

Please sign in to comment.