Last active
October 25, 2017 01:39
-
-
Save withinboredom/9f00394b7066700f504b7a18432221ae to your computer and use it in GitHub Desktop.
Event Sourcing
This file contains hidden or 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
class User extends LiveActor { | |
constructor( id, injectionContainer ) { | |
super( id, injectionContainer ); | |
// set initial state for all users | |
this._state[ 'logged_in' ] = false; | |
} | |
/** | |
* Login a user | |
*/ | |
DoLoginAttempt( password, browserFingerprint ) { | |
if ( this._state[ 'too_many_tries' ] ) { | |
return this.Fire( 'too_many_tries' ); | |
} | |
if ( this._state[ 'password' ] === password ) { | |
const token = this.generateToken(); | |
this.Fire( 'logged_in', { | |
browserFingerprint, | |
token | |
} ); | |
return token; | |
} else { | |
return this.Fire( 'invalid_login', { | |
browserFingerprint | |
} ); | |
} | |
} | |
/* Event handlers follow */ | |
/* | |
* Handle an invalid login attempt | |
*/ | |
invalid_login( data ) { | |
// do logic | |
} | |
} |
This file contains hidden or 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
await Given( 'A user logs in for the first time', [ | |
{ | |
name: 'signup', | |
data: { | |
username: 'test', | |
password: 'test' | |
} | |
} | |
] ).When( User, 'DoLogin', 'test', {} ).Then( [ | |
{ | |
name: 'logged_in', | |
data: { | |
browserFingerprint: {}, | |
token: '{string}' | |
} | |
} | |
] ).AndState( { | |
logged_in: true, | |
invalid_password_tries_in_last_period: 0 | |
// etc | |
} ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment