-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user): add basic auth service and lognup page
- Loading branch information
Showing
10 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function userLogin (context, payload) { | ||
context.dispatch('USER_LOGNUP', payload); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
|
||
import connectToStores from 'fluxible-addons-react/connectToStores'; | ||
import AuthStore from '../../stores/AuthStore'; | ||
|
||
import userLognup from '../../actions/userLognup'; | ||
|
||
class Login extends React.Component { | ||
|
||
constructor (props) { | ||
super(props); | ||
this.state = { email: '' }; | ||
} | ||
|
||
lognup () { | ||
this.props.context.executeAction(userLognup, this.state.email); | ||
} | ||
|
||
handleInput (e) { | ||
if (e.which === 13) { return this.lognup(); } | ||
this.setState({ email: e.target.value }); | ||
} | ||
|
||
render () { | ||
return ( | ||
<div className='Login'> | ||
<p>Enter your email to login.</p> | ||
<input | ||
value={this.state.email} | ||
onChange={this.handleInput.bind(this)} | ||
onKeyDown={this.handleInput.bind(this)} | ||
type='text' placeholder='Email'/> | ||
<p>{this.props.lognupMessage}</p> | ||
</div> | ||
); | ||
} | ||
|
||
} | ||
|
||
export default connectToStores(Login, [AuthStore], (context) => { | ||
let authStore = context.getStore(AuthStore); | ||
return { | ||
isLogged: authStore.isLogged(), | ||
lognupMessage: authStore.getLognupMessage() | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import BaseStore from 'fluxible/addons/BaseStore'; | ||
|
||
export default class AuthStore extends BaseStore { | ||
|
||
static getToken () { | ||
let val = '; ' + document.cookie; | ||
let parts = val.split('; token='); | ||
if (parts.length !== 2) { return null; } | ||
return parts.pop().split(';').shift(); | ||
} | ||
|
||
constructor (dispatcher) { | ||
super(dispatcher); | ||
this._user = null; | ||
this._jwt = null; | ||
this._lognupMessage = null; | ||
} | ||
|
||
handleLognup (email) { | ||
|
||
fetch('/api/users', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ email }) | ||
}) | ||
.then(res => { return res.json(); }) | ||
.then(data => { | ||
this._lognupMessage = data.message; | ||
this.emitChange(); | ||
}); | ||
|
||
} | ||
|
||
handleLogin () { | ||
fetch('/api/users/me', { | ||
method: 'GET', | ||
headers: { Authorization: 'Bearer ' + 'tttt' } | ||
}) | ||
.then(user => { | ||
console.log(user); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
this._jwt = jwt; | ||
this._user = {}; | ||
this.emitChange(); | ||
} | ||
|
||
handleLogout () { | ||
this._jwt = null; | ||
this._user = null; | ||
this.emitChange(); | ||
} | ||
|
||
getUser () { | ||
return this._user; | ||
} | ||
|
||
getLognupMessage () { | ||
return this._lognupMessage; | ||
} | ||
|
||
isLogged () { | ||
return !!this._user; | ||
} | ||
|
||
} | ||
|
||
AuthStore.storeName = 'AuthStore'; | ||
|
||
AuthStore.handlers = { | ||
USER_LOGNUP: 'handleLognup', | ||
USER_LOGIN: 'handleLogin', | ||
USER_LOGOUT: 'handleLogout' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters