Skip to content

Commit 18edee4

Browse files
committed
feat(): saveCredentials() an unsaveCredentials() function are now available through public API
1 parent ca71235 commit 18edee4

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

projects/ng-strapi-auth/src/lib/ng-strapi-auth.service.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Subject, Observable } from 'rxjs';
88
providedIn: 'root'
99
})
1010
export class NgStrapiAuthService {
11-
1211
private apiUrl: string = undefined;
1312
private authStateChangesSubject: Subject<boolean> = new Subject();
1413
private userChangesSubject: Subject<any> = new Subject();
@@ -35,8 +34,7 @@ export class NgStrapiAuthService {
3534
}
3635
public authenticated = false;
3736

38-
39-
constructor(
37+
constructor(
4038
@Inject('config') private config: NgStrapiAuthConfig,
4139
private httpClient: HttpClient
4240
) {
@@ -54,7 +52,9 @@ export class NgStrapiAuthService {
5452
}
5553

5654
async autoSignIn() {
57-
if (!this.apiUrl) { throw new Error('[NgStrapiAuth]: no api url provided'); }
55+
if (!this.apiUrl) {
56+
throw new Error('[NgStrapiAuth]: no api url provided');
57+
}
5858

5959
const credentials = this.getSavedCredentials();
6060

@@ -65,17 +65,23 @@ export class NgStrapiAuthService {
6565
this.authStateChangesSubject.next(this.authenticated);
6666

6767
return this.user;
68-
6968
} else {
7069
throw new Error('[NgStrapiAuth]: no user auto signed in');
7170
}
7271
}
7372

7473
async signIn(username: string, password: string) {
75-
if (!this.apiUrl) { throw new Error('[NgStrapiAuth]: no api url provided'); }
74+
if (!this.apiUrl) {
75+
throw new Error('[NgStrapiAuth]: no api url provided');
76+
}
7677

7778
try {
78-
const res: any = await this.httpClient.post(this.apiUrl + '/auth/local', { identifier: username, password: password }).toPromise();
79+
const res: any = await this.httpClient
80+
.post(this.apiUrl + '/auth/local', {
81+
identifier: username,
82+
password: password
83+
})
84+
.toPromise();
7985

8086
this.user = res.user;
8187
this.jwt = res.jwt;
@@ -84,7 +90,6 @@ export class NgStrapiAuthService {
8490
this.authStateChangesSubject.next(this.authenticated);
8591

8692
return this.user;
87-
8893
} catch (err) {
8994
throw err;
9095
}
@@ -101,10 +106,18 @@ export class NgStrapiAuthService {
101106
}
102107

103108
async register(username: string, email: string, password: string) {
104-
if (!this.apiUrl) { throw new Error('[NgStrapiAuth]: no api url provided'); }
109+
if (!this.apiUrl) {
110+
throw new Error('[NgStrapiAuth]: no api url provided');
111+
}
105112

106113
try {
107-
const res: any = await this.httpClient.post(this.apiUrl + '/auth/local/register', { username: username, email: email, password: password }).toPromise();
114+
const res: any = await this.httpClient
115+
.post(this.apiUrl + '/auth/local/register', {
116+
username: username,
117+
email: email,
118+
password: password
119+
})
120+
.toPromise();
108121

109122
this.user = res.user;
110123
this.jwt = res.jwt;
@@ -113,12 +126,25 @@ export class NgStrapiAuthService {
113126
this.authStateChangesSubject.next(this.authenticated);
114127

115128
return this.user;
116-
117129
} catch (err) {
118130
throw err;
119131
}
120132
}
121133

134+
async saveCredentials() {
135+
if (this.user) {
136+
localStorage.setItem('current-user', JSON.stringify(this.user));
137+
}
138+
if (this.jwt) {
139+
localStorage.setItem('current-user-jwt', JSON.stringify(this.jwt));
140+
}
141+
}
142+
143+
async unsaveCredentials() {
144+
localStorage.removeItem('current-user');
145+
localStorage.removeItem('current-user-jwt');
146+
}
147+
122148
private getSavedCredentials() {
123149
const user = localStorage.getItem('current-user');
124150
const jwt = localStorage.getItem('current-user-jwt');
@@ -132,19 +158,4 @@ export class NgStrapiAuthService {
132158
return undefined;
133159
}
134160
}
135-
136-
private saveCredentials() {
137-
if (this.user) {
138-
localStorage.setItem('current-user', JSON.stringify(this.user));
139-
}
140-
if (this.jwt) {
141-
localStorage.setItem('current-user-jwt', JSON.stringify(this.jwt));
142-
}
143-
}
144-
145-
private unsaveCredentials() {
146-
localStorage.removeItem('current-user');
147-
localStorage.removeItem('current-user-jwt');
148-
}
149-
150161
}

0 commit comments

Comments
 (0)