Skip to content

Commit a99b748

Browse files
authored
Merge pull request #110 from CacherIO/master
Enable the use of non-origin oAuth urls + new window options
2 parents 4922ad4 + 46ecdea commit a99b748

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ constructor(private _tokenService: Angular2TokenService) {
130130
resetPasswordPath: 'auth/password',
131131
resetPasswordCallback: window.location.href,
132132
133+
oAuthHost: window.location.origin,
133134
oAuthPaths: {
134135
github: 'auth/github'
135136
},
136137
oAuthCallbackPath: 'oauth_callback',
137138
oAuthWindowType: 'newWindow',
139+
oAuthWindowOptions: null,
138140
139141
userTypes: null,
140142
@@ -150,6 +152,7 @@ constructor(private _tokenService: Angular2TokenService) {
150152
151153
| Options | Description |
152154
| --------------------------------------- | ---------------------------------------- |
155+
| `apiBase?: string` | Sets the server for all API calls. |
153156
| `apiPath?: string` | Sets base path all operations are based on |
154157
| `signInPath?: string` | Sets path for sign in |
155158
| `signInRedirect?: string` | Sets redirect path for failed CanActivate |
@@ -165,9 +168,11 @@ constructor(private _tokenService: Angular2TokenService) {
165168
| `resetPasswordCallback?: string` | Sets the path user are redirected to after email confirmation for password reset |
166169
| `userTypes?: UserTypes[]` | Allows the configuration of multiple user types (see [Multiple User Types](#multiple-user-types)) |
167170
| `globalOptions?: GlobalOptions` | Allows the configuration of global options (see below) |
171+
| `oAuthBase?: string` | Configure the OAuth server (used for backends on a different url) |
168172
| `oAuthPaths?: { [key:string]: string }` | Sets paths for sign in with OAuth |
169173
| `oAuthCallbackPath?: string` | Sets path for OAuth sameWindow callback |
170174
| `oAuthWindowType?:`string` | Window type for Oauth authentication |
175+
| `oAuthWindowOptions?: { [key:string]: string }` | Set additional options to pass into `window.open()` |
171176
### Global Options
172177
| Options | Description |
173178
| ------------------------------------- | ----------------------------------------------- |
@@ -255,14 +260,15 @@ this._tokenService.validateToken().subscribe(
255260
### .updatePassword()
256261
Updates the password for the logged in user.
257262
258-
`updatePassword({password: string, passwordConfirmation: string, currentPassword?: string, userType?: string}): Observable<Response>`
263+
`updatePassword({password: string, passwordConfirmation: string, currentPassword?: string, userType?: string, resetPasswordToken?: string}): Observable<Response>`
259264
260265
#### Example:
261266
```javascript
262267
this._tokenService.updatePassword({
263268
password: 'newPassword',
264269
passwordConfirmation: 'newPassword',
265-
passwordCurrent: 'oldPassword'
270+
passwordCurrent: 'oldPassword',
271+
resetPasswordToken: 'resetPasswordToken',
266272
}).subscribe(
267273
res => console.log(res),
268274
error => console.log(error)

src/angular2-token.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface RegisterData {
1010
email: string;
1111
password: string;
1212
passwordConfirmation: string;
13+
name?: string;
1314
userType?: string;
1415
}
1516

@@ -18,6 +19,7 @@ export interface UpdatePasswordData {
1819
passwordConfirmation: string;
1920
passwordCurrent: string;
2021
userType?: string;
22+
resetPasswordToken?: string;
2123
}
2224

2325
export interface ResetPasswordData {
@@ -57,6 +59,7 @@ export interface GlobalOptions {
5759
}
5860

5961
export interface Angular2TokenOptions {
62+
apiBase?: string;
6063
apiPath?: string;
6164

6265
signInPath?: string;
@@ -78,9 +81,11 @@ export interface Angular2TokenOptions {
7881

7982
userTypes?: UserType[];
8083

84+
oAuthBase?: string;
8185
oAuthPaths?: { [key:string]: string; };
8286
oAuthCallbackPath?: string;
8387
oAuthWindowType?: string;
88+
oAuthWindowOptions?: { [key:string]: string; };
8489

8590
globalOptions?: GlobalOptions;
8691
}

src/angular2-token.service.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class Angular2TokenService implements CanActivate {
102102

103103
let defaultOptions: Angular2TokenOptions = {
104104
apiPath: null,
105+
apiBase: '',
105106

106107
signInPath: 'auth/sign_in',
107108
signInRedirect: null,
@@ -122,11 +123,14 @@ export class Angular2TokenService implements CanActivate {
122123

123124
userTypes: null,
124125

126+
oAuthBase: window.location.origin,
125127
oAuthPaths: {
126128
github: 'auth/github'
127129
},
128130
oAuthCallbackPath: 'oauth_callback',
129131
oAuthWindowType: 'newWindow',
132+
oAuthWindowOptions: null,
133+
130134
globalOptions: {
131135
headers: {
132136
'Content-Type': 'application/json',
@@ -150,6 +154,7 @@ export class Angular2TokenService implements CanActivate {
150154

151155
let body = JSON.stringify({
152156
email: registerData.email,
157+
name: registerData.name,
153158
password: registerData.password,
154159
password_confirmation: registerData.passwordConfirmation,
155160
confirm_success_url: this._options.registerAccountCallback
@@ -191,7 +196,20 @@ export class Angular2TokenService implements CanActivate {
191196
let authUrl: string = this._buildOAuthUrl(oAuthPath, callbackUrl, oAuthWindowType);
192197

193198
if (oAuthWindowType == 'newWindow') {
194-
let popup = window.open(authUrl, '_blank', 'closebuttoncaption=Cancel');
199+
let oAuthWindowOptions = this._options.oAuthWindowOptions;
200+
let windowOptions = '';
201+
202+
if (oAuthWindowOptions) {
203+
for (let key in oAuthWindowOptions) {
204+
windowOptions += `,${key}=${oAuthWindowOptions[key]}`;
205+
}
206+
}
207+
208+
let popup = window.open(
209+
authUrl,
210+
'_blank',
211+
`closebuttoncaption=Cancel${windowOptions}`
212+
);
195213
return this._requestCredentialsViaPostMessage(popup);
196214
} else if (oAuthWindowType == 'sameWindow') {
197215
window.location.href = authUrl;
@@ -242,21 +260,26 @@ export class Angular2TokenService implements CanActivate {
242260
if (updatePasswordData.userType != null)
243261
this._currentUserType = this._getUserTypeByName(updatePasswordData.userType);
244262

245-
let body: string;
263+
let args: any;
246264

247265
if (updatePasswordData.passwordCurrent == null) {
248-
body = JSON.stringify({
266+
args = {
249267
password: updatePasswordData.password,
250268
password_confirmation: updatePasswordData.passwordConfirmation
251-
});
269+
}
252270
} else {
253-
body = JSON.stringify({
271+
args = {
254272
current_password: updatePasswordData.passwordCurrent,
255273
password: updatePasswordData.password,
256274
password_confirmation: updatePasswordData.passwordConfirmation
257-
});
275+
};
276+
}
277+
278+
if (updatePasswordData.resetPasswordToken) {
279+
args.reset_password_token = updatePasswordData.resetPasswordToken;
258280
}
259281

282+
let body = JSON.stringify(args);
260283
return this.put(this._constructUserPath() + this._options.updatePasswordPath, body);
261284
}
262285

@@ -505,16 +528,16 @@ export class Angular2TokenService implements CanActivate {
505528

506529
private _constructUserPath(): string {
507530
if (this._currentUserType == null)
508-
return '';
531+
return '/';
509532
else
510533
return this._currentUserType.path + '/';
511534
}
512535

513536
private _constructApiPath(): string {
514537
if (this._options.apiPath == null)
515-
return '';
538+
return this._options.apiBase + '/';
516539
else
517-
return this._options.apiPath + '/';
540+
return this._options.apiBase + '/' + this._options.apiPath + '/';
518541
}
519542

520543
private _getOAuthPath(oAuthType: string): string {
@@ -523,20 +546,20 @@ export class Angular2TokenService implements CanActivate {
523546
oAuthPath = this._options.oAuthPaths[oAuthType];
524547

525548
if (oAuthPath == null)
526-
oAuthPath = '/auth/${oAuthType}';
549+
oAuthPath = `/auth/${oAuthType}`;
527550

528551
return oAuthPath;
529552
}
530553

531554
private _buildOAuthUrl(oAuthPath: string, callbackUrl: string, windowType: string): string {
532555
let url: string;
533556

534-
url = '${window.location.origin}/${oAuthPath}';
535-
url += '?omniauth_window_type=${windowType}';
536-
url += '&auth_origin_url=${encodeURIComponent(callbackUrl)}';
557+
url = `${this._options.oAuthBase}/${oAuthPath}`;
558+
url += `?omniauth_window_type=${windowType}`;
559+
url += `&auth_origin_url=${encodeURIComponent(callbackUrl)}`;
537560

538561
if (this._currentUserType != null)
539-
url += '&resource_class=${this._currentUserType.name}';
562+
url += `&resource_class=${this._currentUserType.name}`;
540563

541564
return url;
542565
}
@@ -545,7 +568,7 @@ export class Angular2TokenService implements CanActivate {
545568
let pollerObserv = Observable.interval(500);
546569

547570
let responseObserv = Observable.fromEvent(window, 'message').pluck('data')
548-
.filter(this._oAuthWindowResponseFilter);
571+
.filter(this._oAuthWindowResponseFilter);
549572

550573
let responseSubscription = responseObserv.subscribe(
551574
this._getAuthDataFromPostMessage.bind(this)

0 commit comments

Comments
 (0)