Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
update login
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Apr 21, 2018
1 parent 0f868f6 commit a483127
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ const riot = require('riot');
const route = require('riot-route').default;
const WebSocketEvents = require('./helpers/web-socket-events');
const StreamingRest = require('./helpers/streaming-rest');
const fetchJson = require('./helpers/fetch-json');

(async () => {

const mixin = {};

// csrf

const csrfElement = document.getElementsByName('frost-csrf').item(0);
if (csrfElement != null)
mixin.csrf = csrfElement.content;

// userId

const userIdElement = document.getElementsByName('frost-userId').item(0);
Expand All @@ -22,7 +29,9 @@ const StreamingRest = require('./helpers/streaming-rest');

let webSocket;
try {
webSocket = await WebSocketEvents.connect(`${secure ? 'wss' : 'ws'}://${location.host}`);
const apiHost = 'localhost:8000'; // TODO
const accessToken = localStorage.getItem('accessToken');
webSocket = await WebSocketEvents.connect(`${secure ? 'wss' : 'ws'}://${apiHost}?access_token=${accessToken}`);
webSocket.addEventListener('close', (ev) => { console.log('close:', ev); });
webSocket.addEventListener('error', (ev) => { console.log('error:', ev); });
WebSocketEvents.init(webSocket);
Expand All @@ -31,6 +40,10 @@ const StreamingRest = require('./helpers/streaming-rest');
catch (err) {
alert('WebSocketの接続に失敗しました');
console.log(err);
// 念のためログアウト
await fetchJson('DELETE', '/session', {
_csrf: mixin.csrf
});
return;
}

Expand All @@ -52,12 +65,6 @@ const StreamingRest = require('./helpers/streaming-rest');
if (siteKeyElement != null)
mixin.siteKey = siteKeyElement.content;

// csrf

const csrfElement = document.getElementsByName('frost-csrf').item(0);
if (csrfElement != null)
mixin.csrf = csrfElement.content;

// central observer

mixin.central = riot.observable();
Expand Down
3 changes: 3 additions & 0 deletions src/client/tags/frost-form-login.tag
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
_csrf: this.csrf
})
.then(async (res) => {
const json = await res.json();

if (res.ok) {
localStorage.setItem('accessToken', json.accessToken);
location.reload();
return;
}
Expand Down

0 comments on commit a483127

Please sign in to comment.