Skip to content

Commit 6b03602

Browse files
committed
Added password reset method
1 parent f25354d commit 6b03602

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ constructor(private _tokenService: Angular2TokenService) {
6060
apiPath: null,
6161
signInPath: 'auth/sign_in',
6262
signOutPath: 'auth/sign_out',
63+
validateTokenPath: 'auth/validate_token',
6364
registerAccountPath: 'auth',
6465
deleteAccountPath: 'auth',
65-
validateTokenPath: 'auth/validate_token',
66+
emailRegistrationPath: window.location.href,
6667
updatePasswordPath: 'auth/password',
68+
resetPasswordPath: 'auth/password',
69+
emailPasswordPath: window.location.href,
6770
userTypes: null
6871
});
6972
}
@@ -73,10 +76,13 @@ constructor(private _tokenService: Angular2TokenService) {
7376
- `apiPath?: string` - Sets base path all operations are based on
7477
- `signInPath?: string` - Sets path for sign in
7578
- `signOutPath?: string` - Sets path for sign out
79+
- `validateTokenPath?: string` - Sets path for token validation
7680
- `registerAccountPath?: string` - Sets path for account registration
7781
- `deleteAccountPath?: string` - Sets path for account deletion
78-
- `validateTokenPath?: string` - Sets path for token validation
82+
- `emailRegistrationPath?: string` - Sets the path user are redirected to after email confirmation for registration
7983
- `updatePasswordPath?: string` - Sets path for password update
84+
- `resetPasswordPath?: string` - Sets path for password reset
85+
- `emailPasswordPath?: string` - Sets the path user are redirected to after email confirmation for password reset
8086
- `userTypes?: UserTypes[]` - Allows the configuration of multiple user types
8187
8288
Further information on paths/routes can be found at

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-token",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Angular2 service for token based authentication",
55
"main": "./angular2-token.js",
66
"typings": "./angular2-token.d.ts",

src/angular2-token.model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export interface Angular2TokenOptions {
1717
signOutPath?: string;
1818
registerAccountPath?: string,
1919
deleteAccountPath?: string,
20+
emailRegistrationPath?: string,
2021
validateTokenPath?: string;
2122
updatePasswordPath?: string;
23+
resetPasswordPath?: string;
24+
emailPasswordPath?: string;
2225
userTypes?: UserType[];
2326
}

src/angular2-token.service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ export class Angular2TokenService {
3333
apiPath: null,
3434
signInPath: 'auth/sign_in',
3535
signOutPath: 'auth/sign_out',
36+
validateTokenPath: 'auth/validate_token',
3637
registerAccountPath: 'auth',
3738
deleteAccountPath: 'auth',
38-
validateTokenPath: 'auth/validate_token',
39+
emailRegistrationPath: window.location.href,
3940
updatePasswordPath: 'auth/password',
41+
resetPasswordPath: 'auth/password',
42+
emailPasswordPath: window.location.href,
4043
userTypes: null
4144
};
4245

@@ -56,7 +59,8 @@ export class Angular2TokenService {
5659
let body = JSON.stringify({
5760
email: email,
5861
password: password,
59-
password_confirmation: passwordConfirmation
62+
password_confirmation: passwordConfirmation,
63+
confirm_success_url: this._options.emailRegistrationPath
6064
});
6165

6266
return this.post(this._constructUserPath() + this._options.registerAccountPath, body).map(res => res.json());
@@ -111,6 +115,17 @@ export class Angular2TokenService {
111115
return this.put(this._constructUserPath() + this._options.updatePasswordPath, body).map(res => res.json());
112116
}
113117

118+
// Reset password request
119+
resetPassword(email: string): Observable<Response> {
120+
121+
let body = JSON.stringify({
122+
email: email,
123+
redirect_url: this._options.emailPasswordPath
124+
});
125+
126+
return this.post(this._constructUserPath() + this._options.resetPasswordPath, body).map(res => res.json());
127+
}
128+
114129
// Standard HTTP requests
115130
get(path: string, data?: any): Observable<Response> {
116131
return this._sendHttpRequest(RequestMethod.Get, path, data);

src/angular2-token.spec.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ describe('Angular2TokenService', () => {
3131
password: 'password'
3232
}
3333

34+
let registerData = {
35+
email: 'test@test.de',
36+
password: 'password',
37+
password_confirmation: 'password',
38+
confirm_success_url: window.location.href
39+
}
40+
3441
beforeEach(() => {
3542
// Inject HTTP and Angular2TokenService
3643
addProviders([
@@ -63,39 +70,63 @@ describe('Angular2TokenService', () => {
6370
});
6471
});
6572

66-
it('signIn method should post data', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
73+
// Testing Default Configuration
74+
75+
it('signIn method should post data to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
6776

6877
mockBackend.connections.subscribe(
6978
c => {
7079
expect(c.request.getBody()).toEqual(JSON.stringify(signInData));
7180
expect(c.request.method).toEqual(RequestMethod.Post);
81+
expect(c.request.url).toEqual('auth/sign_in');
7282
}
7383
);
7484

7585
tokenService.init();
7686
tokenService.signIn(signInData.email, signInData.password);
7787
}));
7888

79-
it('signIn method should send to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
89+
it('signOut method should delete to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
8090

8191
mockBackend.connections.subscribe(
82-
c => expect(c.request.url).toEqual('auth/sign_in')
92+
c => {
93+
expect(c.request.method).toEqual(RequestMethod.Delete);
94+
expect(c.request.url).toEqual('auth/sign_out');
95+
}
8396
);
8497

8598
tokenService.init();
86-
tokenService.signIn(signInData.email, signInData.password);
99+
tokenService.signOut();
87100
}));
88101

102+
it('registerAccount should post data to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
103+
104+
mockBackend.connections.subscribe(
105+
c => {
106+
expect(c.request.getBody()).toEqual(JSON.stringify(registerData));
107+
expect(c.request.method).toEqual(RequestMethod.Post);
108+
expect(c.request.url).toEqual('auth');
109+
}
110+
);
111+
112+
tokenService.init();
113+
tokenService.registerAccount(registerData.email, registerData.password, registerData.password_confirmation);
114+
}));
115+
116+
// Testing Configured Configuration
117+
89118
it('signIn method should send to configured api path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
90119

91120
mockBackend.connections.subscribe(
92121
c => expect(c.request.url).toEqual('myapi/auth/sign_in')
93122
);
94123

95-
tokenService.init({apiPath: 'myapi'});
124+
tokenService.init({ apiPath: 'myapi' });
96125
tokenService.signIn(signInData.email, signInData.password);
97126
}));
98127

128+
// Testing Token handling
129+
99130
it('signIn method should receive headers and set local storage', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
100131

101132
mockBackend.connections.subscribe(
@@ -116,16 +147,6 @@ describe('Angular2TokenService', () => {
116147
expect(localStorage.getItem('uid')).toEqual(uid);
117148
}));
118149

119-
it('signOut method should send delete', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
120-
121-
mockBackend.connections.subscribe(
122-
c => expect(c.request.method).toEqual(RequestMethod.Delete)
123-
);
124-
125-
tokenService.init();
126-
tokenService.signOut();
127-
}));
128-
129150
it('signOut method should clear local storage', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
130151
localStorage.setItem('token-type', tokenType);
131152
localStorage.setItem('uid', uid);
@@ -147,4 +168,4 @@ describe('Angular2TokenService', () => {
147168
expect(localStorage.getItem('uid')).toBe(null);
148169
}));
149170

150-
});
171+
});

0 commit comments

Comments
 (0)