@@ -10,15 +10,23 @@ var AuthStore = Reflux.createStore({
1010 this . listenTo ( login , this . login )
1111 this . listenTo ( register , this . register )
1212 } ,
13+ // Reflux Store
1314 login : function ( input ) {
1415 request
1516 . post ( 'http://localhost:8000/auth/login' )
1617 . send ( input )
1718 . set ( 'Accept' , 'application/json' )
1819 . end ( function ( err , res ) {
19- if ( err ) console . error ( err )
20+ if ( err ) {
21+ console . error ( err )
22+ this . trigger ( null )
23+ return
24+ }
25+
2026 var user = res . body . user
2127 localStorage . setItem ( 'token' , res . body . token )
28+ localStorage . setItem ( 'user' , JSON . stringify ( res . body . user ) )
29+
2230 this . trigger ( user )
2331 } . bind ( this ) )
2432 } ,
@@ -28,11 +36,28 @@ var AuthStore = Reflux.createStore({
2836 . send ( input )
2937 . set ( 'Accept' , 'application/json' )
3038 . end ( function ( err , res ) {
31- if ( err ) console . error ( err )
39+ if ( err ) {
40+ console . error ( err )
41+ this . trigger ( null )
42+ return
43+ }
44+
3245 var user = res . body . user
3346 localStorage . setItem ( 'token' , res . body . token )
47+ localStorage . setItem ( 'user' , JSON . stringify ( res . body . user ) )
48+
3449 this . trigger ( user )
3550 } . bind ( this ) )
51+ } ,
52+ // Methods
53+ check : function ( ) {
54+ if ( localStorage . getItem ( 'token' ) ) return true
55+ return false
56+ } ,
57+ getUser : function ( ) {
58+ var userJSON = localStorage . getItem ( 'user' )
59+ if ( userJSON == null ) return null
60+ return JSON . parse ( userJSON )
3661 }
3762} )
3863
0 commit comments