@@ -8,7 +8,6 @@ import { Subject, Observable } from 'rxjs';
8
8
providedIn : 'root'
9
9
} )
10
10
export class NgStrapiAuthService {
11
-
12
11
private apiUrl : string = undefined ;
13
12
private authStateChangesSubject : Subject < boolean > = new Subject ( ) ;
14
13
private userChangesSubject : Subject < any > = new Subject ( ) ;
@@ -35,8 +34,7 @@ export class NgStrapiAuthService {
35
34
}
36
35
public authenticated = false ;
37
36
38
-
39
- constructor (
37
+ constructor (
40
38
@Inject ( 'config' ) private config : NgStrapiAuthConfig ,
41
39
private httpClient : HttpClient
42
40
) {
@@ -54,7 +52,9 @@ export class NgStrapiAuthService {
54
52
}
55
53
56
54
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
+ }
58
58
59
59
const credentials = this . getSavedCredentials ( ) ;
60
60
@@ -65,17 +65,23 @@ export class NgStrapiAuthService {
65
65
this . authStateChangesSubject . next ( this . authenticated ) ;
66
66
67
67
return this . user ;
68
-
69
68
} else {
70
69
throw new Error ( '[NgStrapiAuth]: no user auto signed in' ) ;
71
70
}
72
71
}
73
72
74
73
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
+ }
76
77
77
78
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 ( ) ;
79
85
80
86
this . user = res . user ;
81
87
this . jwt = res . jwt ;
@@ -84,7 +90,6 @@ export class NgStrapiAuthService {
84
90
this . authStateChangesSubject . next ( this . authenticated ) ;
85
91
86
92
return this . user ;
87
-
88
93
} catch ( err ) {
89
94
throw err ;
90
95
}
@@ -101,10 +106,18 @@ export class NgStrapiAuthService {
101
106
}
102
107
103
108
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
+ }
105
112
106
113
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 ( ) ;
108
121
109
122
this . user = res . user ;
110
123
this . jwt = res . jwt ;
@@ -113,12 +126,25 @@ export class NgStrapiAuthService {
113
126
this . authStateChangesSubject . next ( this . authenticated ) ;
114
127
115
128
return this . user ;
116
-
117
129
} catch ( err ) {
118
130
throw err ;
119
131
}
120
132
}
121
133
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
+
122
148
private getSavedCredentials ( ) {
123
149
const user = localStorage . getItem ( 'current-user' ) ;
124
150
const jwt = localStorage . getItem ( 'current-user-jwt' ) ;
@@ -132,19 +158,4 @@ export class NgStrapiAuthService {
132
158
return undefined ;
133
159
}
134
160
}
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
-
150
161
}
0 commit comments