Skip to content

Commit

Permalink
Merge pull request #2 from gilhardl/feature/public-api
Browse files Browse the repository at this point in the history
Upgrade module's public API
  • Loading branch information
gilhardl authored Apr 15, 2019
2 parents afc55e9 + 18edee4 commit d2ba448
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions projects/ng-strapi-auth/src/lib/ng-strapi-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Subject, Observable } from 'rxjs';
providedIn: 'root'
})
export class NgStrapiAuthService {

private apiUrl: string = undefined;
private authStateChangesSubject: Subject<boolean> = new Subject();
private userChangesSubject: Subject<any> = new Subject();
Expand All @@ -35,8 +34,7 @@ export class NgStrapiAuthService {
}
public authenticated = false;


constructor(
constructor(
@Inject('config') private config: NgStrapiAuthConfig,
private httpClient: HttpClient
) {
Expand All @@ -54,7 +52,9 @@ export class NgStrapiAuthService {
}

async autoSignIn() {
if (!this.apiUrl) { throw new Error('[NgStrapiAuth]: no api url provided'); }
if (!this.apiUrl) {
throw new Error('[NgStrapiAuth]: no api url provided');
}

const credentials = this.getSavedCredentials();

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

return this.user;

} else {
throw new Error('[NgStrapiAuth]: no user auto signed in');
}
}

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

try {
const res: any = await this.httpClient.post(this.apiUrl + '/auth/local', { identifier: username, password: password }).toPromise();
const res: any = await this.httpClient
.post(this.apiUrl + '/auth/local', {
identifier: username,
password: password
})
.toPromise();

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

return this.user;

} catch (err) {
throw err;
}
Expand All @@ -101,10 +106,18 @@ export class NgStrapiAuthService {
}

async register(username: string, email: string, password: string) {
if (!this.apiUrl) { throw new Error('[NgStrapiAuth]: no api url provided'); }
if (!this.apiUrl) {
throw new Error('[NgStrapiAuth]: no api url provided');
}

try {
const res: any = await this.httpClient.post(this.apiUrl + '/auth/local/register', { username: username, email: email, password: password }).toPromise();
const res: any = await this.httpClient
.post(this.apiUrl + '/auth/local/register', {
username: username,
email: email,
password: password
})
.toPromise();

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

return this.user;

} catch (err) {
throw err;
}
}

async saveCredentials() {
if (this.user) {
localStorage.setItem('current-user', JSON.stringify(this.user));
}
if (this.jwt) {
localStorage.setItem('current-user-jwt', JSON.stringify(this.jwt));
}
}

async unsaveCredentials() {
localStorage.removeItem('current-user');
localStorage.removeItem('current-user-jwt');
}

private getSavedCredentials() {
const user = localStorage.getItem('current-user');
const jwt = localStorage.getItem('current-user-jwt');
Expand All @@ -132,19 +158,4 @@ export class NgStrapiAuthService {
return undefined;
}
}

private saveCredentials() {
if (this.user) {
localStorage.setItem('current-user', JSON.stringify(this.user));
}
if (this.jwt) {
localStorage.setItem('current-user-jwt', JSON.stringify(this.jwt));
}
}

private unsaveCredentials() {
localStorage.removeItem('current-user');
localStorage.removeItem('current-user-jwt');
}

}

0 comments on commit d2ba448

Please sign in to comment.