Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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 |
Expand All @@ -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 |
| ------------------------------------- | ----------------------------------------------- |
Expand Down Expand Up @@ -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<Response>`
`updatePassword({password: string, passwordConfirmation: string, currentPassword?: string, userType?: string, resetPasswordToken?: string}): Observable<Response>`

#### Example:
```javascript
this._tokenService.updatePassword({
password: 'newPassword',
passwordConfirmation: 'newPassword',
passwordCurrent: 'oldPassword'
passwordCurrent: 'oldPassword',
resetPasswordToken: 'resetPasswordToken',
}).subscribe(
res => console.log(res),
error => console.log(error)
Expand Down
5 changes: 5 additions & 0 deletions src/angular2-token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface RegisterData {
email: string;
password: string;
passwordConfirmation: string;
name?: string;
userType?: string;
}

Expand All @@ -18,6 +19,7 @@ export interface UpdatePasswordData {
passwordConfirmation: string;
passwordCurrent: string;
userType?: string;
resetPasswordToken?: string;
}

export interface ResetPasswordData {
Expand Down Expand Up @@ -57,6 +59,7 @@ export interface GlobalOptions {
}

export interface Angular2TokenOptions {
apiBase?: string;
apiPath?: string;

signInPath?: string;
Expand All @@ -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;
}
53 changes: 38 additions & 15 deletions src/angular2-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class Angular2TokenService implements CanActivate {

let defaultOptions: Angular2TokenOptions = {
apiPath: null,
apiBase: '',

signInPath: 'auth/sign_in',
signInRedirect: null,
Expand All @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 {
Expand All @@ -523,20 +546,20 @@ export class Angular2TokenService implements CanActivate {
oAuthPath = this._options.oAuthPaths[oAuthType];

if (oAuthPath == null)
oAuthPath = '/auth/${oAuthType}';
oAuthPath = `/auth/${oAuthType}`;

return oAuthPath;
}

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;
}
Expand All @@ -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)
Expand Down