2022-01-17 12:34:55 +03:00
import { inject as service } from '@ember/service' ;
2020-01-16 20:01:12 +03:00
// TODO: remove usage of Ember Data's private `Errors` class when refactoring validations
// eslint-disable-next-line
2015-07-08 12:56:07 +03:00
import DS from 'ember-data' ;
2017-08-22 10:53:26 +03:00
import EmberObject from '@ember/object' ;
2017-05-29 21:50:03 +03:00
import RSVP from 'rsvp' ;
2021-04-12 15:21:57 +03:00
import UnauthenticatedRoute from 'ghost-admin/routes/unauthenticated' ;
2018-09-17 18:03:58 +03:00
import ValidationEngine from 'ghost-admin/mixins/validation-engine' ;
2022-01-17 12:34:55 +03:00
import classic from 'ember-classic-decorator' ;
2014-03-10 07:44:08 +04:00
2016-06-30 13:21:47 +03:00
const { Promise } = RSVP ;
2015-10-28 14:36:45 +03:00
const { Errors } = DS ;
2022-01-17 12:34:55 +03:00
export default class SignupRoute extends UnauthenticatedRoute {
@ service ghostPaths ;
@ service notifications ;
@ service session ;
@ service ajax ;
@ service config ;
2015-05-26 05:10:50 +03:00
2015-10-28 14:36:45 +03:00
beforeModel ( ) {
2022-01-17 12:34:55 +03:00
if ( this . session . isAuthenticated ) {
2019-03-06 16:53:54 +03:00
this . notifications . showAlert ( 'You need to sign out to register as a new user.' , { type : 'warn' , delayed : true , key : 'signup.create.already-authenticated' } ) ;
2014-07-03 19:06:07 +04:00
}
2017-02-10 13:34:37 +03:00
2022-01-17 12:34:55 +03:00
super . beforeModel ( ... arguments ) ;
}
2014-09-19 08:50:15 +04:00
2015-10-28 14:36:45 +03:00
model ( params ) {
2022-01-17 12:34:55 +03:00
@ classic
class SignupDetails extends EmberObject . extend ( ValidationEngine ) {
validationType = 'signup' ;
}
2018-09-17 18:03:58 +03:00
let signupDetails = SignupDetails . create ( ) ;
2017-09-11 10:56:11 +03:00
let re = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}|[A-Za-z0-9_-]{3})?$/ ;
2015-10-28 14:36:45 +03:00
let email ,
tokenText ;
2014-09-19 08:50:15 +04:00
2016-01-19 16:03:27 +03:00
return new Promise ( ( resolve ) => {
2014-09-19 08:50:15 +04:00
if ( ! re . test ( params . token ) ) {
2019-03-06 16:53:54 +03:00
this . 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 ] ;
2019-02-11 14:35:27 +03:00
// leave e-mail blank even though we get it from the token because
// we need the user to type it in for Chrome to remember the
// email/password combo properly
signupDetails . set ( 'email' , '' ) ;
2018-01-11 01:57:43 +03:00
signupDetails . set ( 'token' , params . token ) ;
signupDetails . set ( 'errors' , Errors . create ( ) ) ;
2014-09-19 08:50:15 +04:00
2016-01-18 18:37:14 +03:00
let authUrl = this . get ( 'ghostPaths.url' ) . api ( 'authentication' , 'invitation' ) ;
2019-03-06 16:53:54 +03:00
return this . ajax . request ( authUrl , {
2014-08-25 06:34:26 +04:00
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 ) {
2019-03-06 16:53:54 +03:00
this . 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
2017-10-26 13:02:17 +03:00
// set blogTitle, so password validation has access to it
2018-01-11 01:57:43 +03:00
signupDetails . set ( 'blogTitle' , this . get ( 'config.blogTitle' ) ) ;
2017-10-26 13:02:17 +03:00
2018-01-11 01:57:43 +03:00
resolve ( signupDetails ) ;
2015-10-28 14:36:45 +03:00
} ) . catch ( ( ) => {
2018-01-11 01:57:43 +03:00
resolve ( signupDetails ) ;
2014-08-25 06:34:26 +04:00
} ) ;
2014-09-19 08:50:15 +04:00
} ) ;
2022-01-17 12:34:55 +03:00
}
2014-08-25 06:34:26 +04:00
2015-10-28 14:36:45 +03:00
deactivate ( ) {
2022-01-17 12:34:55 +03:00
super . deactivate ( ... arguments ) ;
2014-09-19 08:50:15 +04:00
// clear the properties that hold the sensitive data from the controller
2018-01-11 01:57:43 +03:00
this . controllerFor ( 'signup' ) . get ( 'signupDetails' ) . setProperties ( { email : '' , password : '' , token : '' } ) ;
2014-07-03 19:06:07 +04:00
}
2022-01-17 12:34:55 +03:00
}