2015-02-13 07:22:32 +03:00
import Ember from 'ember' ;
2015-07-08 12:56:07 +03:00
import DS from 'ember-data' ;
2015-05-27 03:41:12 +03:00
import { request as ajax } from 'ic-ajax' ;
2015-10-18 21:17:02 +03:00
import Configuration from 'ember-simple-auth/configuration' ;
2014-03-10 07:44:08 +04:00
import styleBody from 'ghost/mixins/style-body' ;
2015-10-28 14:36:45 +03:00
const { Route , RSVP , inject } = Ember ;
const { Errors } = DS ;
export default Route . extend ( styleBody , {
2014-07-03 19:06:07 +04:00
classNames : [ 'ghost-signup' ] ,
2015-05-13 08:27:59 +03:00
2015-10-28 14:36:45 +03:00
ghostPaths : inject . service ( 'ghost-paths' ) ,
notifications : inject . service ( ) ,
session : inject . service ( ) ,
2015-05-26 05:10:50 +03:00
2015-10-28 14:36:45 +03:00
beforeModel ( ) {
2015-11-15 14:06:49 +03:00
this . _super ( ... arguments ) ;
2015-10-18 21:17:02 +03:00
if ( this . get ( 'session.isAuthenticated' ) ) {
2015-10-07 17:44:23 +03:00
this . get ( 'notifications' ) . showAlert ( 'You need to sign out to register as a new user.' , { type : 'warn' , delayed : true , key : 'signup.create.already-authenticated' } ) ;
2015-10-18 21:17:02 +03:00
this . transitionTo ( Configuration . routeIfAlreadyAuthenticated ) ;
2014-07-03 19:06:07 +04:00
}
} ,
2014-09-19 08:50:15 +04:00
2015-10-28 14:36:45 +03:00
model ( params ) {
let model = Ember . Object . create ( ) ;
let re = /^(?:[A-Za-z0-9_\-]{4})*(?:[A-Za-z0-9_\-]{2}|[A-Za-z0-9_\-]{3})?$/ ;
let email ,
tokenText ;
2014-09-19 08:50:15 +04:00
2015-10-28 14:36:45 +03:00
return new RSVP . Promise ( ( resolve ) => {
2014-09-19 08:50:15 +04:00
if ( ! re . test ( params . token ) ) {
2015-10-28 14:36:45 +03:00
this . get ( 'notifications' ) . showAlert ( 'Invalid token.' , { type : 'error' , delayed : true , key : 'signup.create.invalid-token' } ) ;
2014-09-19 08:50:15 +04:00
2015-10-28 14:36:45 +03:00
return resolve ( this . transitionTo ( 'signin' ) ) ;
2014-08-06 19:08:02 +04:00
}
2014-08-25 06:34:26 +04:00
2014-09-19 08:50:15 +04:00
tokenText = atob ( params . token ) ;
email = tokenText . split ( '|' ) [ 1 ] ;
2015-01-12 06:15:13 +03:00
model . set ( 'email' , email ) ;
model . set ( 'token' , params . token ) ;
2015-10-28 14:36:45 +03:00
model . set ( 'errors' , Errors . create ( ) ) ;
2014-09-19 08:50:15 +04:00
2015-05-27 03:41:12 +03:00
return ajax ( {
2015-10-28 14:36:45 +03:00
url : this . get ( 'ghostPaths.url' ) . api ( 'authentication' , 'invitation' ) ,
2014-08-25 06:34:26 +04:00
type : 'GET' ,
dataType : 'json' ,
data : {
2015-10-28 14:36:45 +03:00
email
2014-08-25 06:34:26 +04:00
}
2015-10-28 14:36:45 +03:00
} ) . then ( ( response ) => {
2014-08-25 06:34:26 +04:00
if ( response && response . invitation && response . invitation [ 0 ] . valid === false ) {
2015-10-28 14:36:45 +03:00
this . get ( 'notifications' ) . showAlert ( 'The invitation does not exist or is no longer valid.' , { type : 'warn' , delayed : true , key : 'signup.create.invalid-invitation' } ) ;
2014-09-19 08:50:15 +04:00
2015-10-28 14:36:45 +03:00
return resolve ( this . transitionTo ( 'signin' ) ) ;
2014-08-25 06:34:26 +04:00
}
2014-09-19 08:50:15 +04:00
resolve ( model ) ;
2015-10-28 14:36:45 +03:00
} ) . catch ( ( ) => {
2014-09-19 08:50:15 +04:00
resolve ( model ) ;
2014-08-25 06:34:26 +04:00
} ) ;
2014-09-19 08:50:15 +04:00
} ) ;
} ,
2014-08-25 06:34:26 +04:00
2015-10-28 14:36:45 +03:00
deactivate ( ) {
this . _super ( ... arguments ) ;
2014-09-19 08:50:15 +04:00
// clear the properties that hold the sensitive data from the controller
2014-10-25 01:09:50 +04:00
this . controllerFor ( 'signup' ) . setProperties ( { email : '' , password : '' , token : '' } ) ;
2014-07-03 19:06:07 +04:00
}
2014-03-10 07:44:08 +04:00
} ) ;