diff --git a/README.md b/README.md index 65820994..3c4f25ff 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,13 @@ constructor(private _tokenService: Angular2TokenService) { resetPasswordPath: 'auth/password', resetPasswordCallback: window.location.href, + oAuthHost: window.location.origin, oAuthPaths: { github: 'auth/github' }, oAuthCallbackPath: 'oauth_callback', oAuthWindowType: 'newWindow', + oAuthWindowOptions: null, userTypes: null, @@ -150,6 +152,7 @@ constructor(private _tokenService: Angular2TokenService) { | Options | Description | | --------------------------------------- | ---------------------------------------- | +| `apiBase?: string` | Sets the server for all API calls. | | `apiPath?: string` | Sets base path all operations are based on | | `signInPath?: string` | Sets path for sign in | | `signInRedirect?: string` | Sets redirect path for failed CanActivate | @@ -165,9 +168,11 @@ constructor(private _tokenService: Angular2TokenService) { | `resetPasswordCallback?: string` | Sets the path user are redirected to after email confirmation for password reset | | `userTypes?: UserTypes[]` | Allows the configuration of multiple user types (see [Multiple User Types](#multiple-user-types)) | | `globalOptions?: GlobalOptions` | Allows the configuration of global options (see below) | +| `oAuthBase?: string` | Configure the OAuth server (used for backends on a different url) | | `oAuthPaths?: { [key:string]: string }` | Sets paths for sign in with OAuth | | `oAuthCallbackPath?: string` | Sets path for OAuth sameWindow callback | | `oAuthWindowType?:`string` | Window type for Oauth authentication | +| `oAuthWindowOptions?: { [key:string]: string }` | Set additional options to pass into `window.open()` | ### Global Options | Options | Description | | ------------------------------------- | ----------------------------------------------- | @@ -255,14 +260,15 @@ this._tokenService.validateToken().subscribe( ### .updatePassword() Updates the password for the logged in user. -`updatePassword({password: string, passwordConfirmation: string, currentPassword?: string, userType?: string}): Observable` +`updatePassword({password: string, passwordConfirmation: string, currentPassword?: string, userType?: string, resetPasswordToken?: string}): Observable` #### Example: ```javascript this._tokenService.updatePassword({ password: 'newPassword', passwordConfirmation: 'newPassword', - passwordCurrent: 'oldPassword' + passwordCurrent: 'oldPassword', + resetPasswordToken: 'resetPasswordToken', }).subscribe( res => console.log(res), error => console.log(error) diff --git a/src/angular2-token.model.ts b/src/angular2-token.model.ts index 18ea0084..07eaac2d 100644 --- a/src/angular2-token.model.ts +++ b/src/angular2-token.model.ts @@ -10,6 +10,7 @@ export interface RegisterData { email: string; password: string; passwordConfirmation: string; + name?: string; userType?: string; } @@ -18,6 +19,7 @@ export interface UpdatePasswordData { passwordConfirmation: string; passwordCurrent: string; userType?: string; + resetPasswordToken?: string; } export interface ResetPasswordData { @@ -57,6 +59,7 @@ export interface GlobalOptions { } export interface Angular2TokenOptions { + apiBase?: string; apiPath?: string; signInPath?: string; @@ -78,9 +81,11 @@ export interface Angular2TokenOptions { userTypes?: UserType[]; + oAuthBase?: string; oAuthPaths?: { [key:string]: string; }; oAuthCallbackPath?: string; oAuthWindowType?: string; + oAuthWindowOptions?: { [key:string]: string; }; globalOptions?: GlobalOptions; } \ No newline at end of file diff --git a/src/angular2-token.service.ts b/src/angular2-token.service.ts index 8ff3e1a8..11c2b202 100644 --- a/src/angular2-token.service.ts +++ b/src/angular2-token.service.ts @@ -102,6 +102,7 @@ export class Angular2TokenService implements CanActivate { let defaultOptions: Angular2TokenOptions = { apiPath: null, + apiBase: '', signInPath: 'auth/sign_in', signInRedirect: null, @@ -122,11 +123,14 @@ export class Angular2TokenService implements CanActivate { userTypes: null, + oAuthBase: window.location.origin, oAuthPaths: { github: 'auth/github' }, oAuthCallbackPath: 'oauth_callback', oAuthWindowType: 'newWindow', + oAuthWindowOptions: null, + globalOptions: { headers: { 'Content-Type': 'application/json', @@ -150,6 +154,7 @@ export class Angular2TokenService implements CanActivate { let body = JSON.stringify({ email: registerData.email, + name: registerData.name, password: registerData.password, password_confirmation: registerData.passwordConfirmation, confirm_success_url: this._options.registerAccountCallback @@ -191,7 +196,20 @@ export class Angular2TokenService implements CanActivate { let authUrl: string = this._buildOAuthUrl(oAuthPath, callbackUrl, oAuthWindowType); if (oAuthWindowType == 'newWindow') { - let popup = window.open(authUrl, '_blank', 'closebuttoncaption=Cancel'); + let oAuthWindowOptions = this._options.oAuthWindowOptions; + let windowOptions = ''; + + if (oAuthWindowOptions) { + for (let key in oAuthWindowOptions) { + windowOptions += `,${key}=${oAuthWindowOptions[key]}`; + } + } + + let popup = window.open( + authUrl, + '_blank', + `closebuttoncaption=Cancel${windowOptions}` + ); return this._requestCredentialsViaPostMessage(popup); } else if (oAuthWindowType == 'sameWindow') { window.location.href = authUrl; @@ -242,21 +260,26 @@ export class Angular2TokenService implements CanActivate { if (updatePasswordData.userType != null) this._currentUserType = this._getUserTypeByName(updatePasswordData.userType); - let body: string; + let args: any; if (updatePasswordData.passwordCurrent == null) { - body = JSON.stringify({ + args = { password: updatePasswordData.password, password_confirmation: updatePasswordData.passwordConfirmation - }); + } } else { - body = JSON.stringify({ + args = { current_password: updatePasswordData.passwordCurrent, password: updatePasswordData.password, password_confirmation: updatePasswordData.passwordConfirmation - }); + }; + } + + if (updatePasswordData.resetPasswordToken) { + args.reset_password_token = updatePasswordData.resetPasswordToken; } + let body = JSON.stringify(args); return this.put(this._constructUserPath() + this._options.updatePasswordPath, body); } @@ -505,16 +528,16 @@ export class Angular2TokenService implements CanActivate { private _constructUserPath(): string { if (this._currentUserType == null) - return ''; + return '/'; else return this._currentUserType.path + '/'; } private _constructApiPath(): string { if (this._options.apiPath == null) - return ''; + return this._options.apiBase + '/'; else - return this._options.apiPath + '/'; + return this._options.apiBase + '/' + this._options.apiPath + '/'; } private _getOAuthPath(oAuthType: string): string { @@ -523,7 +546,7 @@ export class Angular2TokenService implements CanActivate { oAuthPath = this._options.oAuthPaths[oAuthType]; if (oAuthPath == null) - oAuthPath = '/auth/${oAuthType}'; + oAuthPath = `/auth/${oAuthType}`; return oAuthPath; } @@ -531,12 +554,12 @@ export class Angular2TokenService implements CanActivate { private _buildOAuthUrl(oAuthPath: string, callbackUrl: string, windowType: string): string { let url: string; - url = '${window.location.origin}/${oAuthPath}'; - url += '?omniauth_window_type=${windowType}'; - url += '&auth_origin_url=${encodeURIComponent(callbackUrl)}'; + url = `${this._options.oAuthBase}/${oAuthPath}`; + url += `?omniauth_window_type=${windowType}`; + url += `&auth_origin_url=${encodeURIComponent(callbackUrl)}`; if (this._currentUserType != null) - url += '&resource_class=${this._currentUserType.name}'; + url += `&resource_class=${this._currentUserType.name}`; return url; } @@ -545,7 +568,7 @@ export class Angular2TokenService implements CanActivate { let pollerObserv = Observable.interval(500); let responseObserv = Observable.fromEvent(window, 'message').pluck('data') - .filter(this._oAuthWindowResponseFilter); + .filter(this._oAuthWindowResponseFilter); let responseSubscription = responseObserv.subscribe( this._getAuthDataFromPostMessage.bind(this)