質問編集履歴
2
リクエスト側のコードの追加、引用先の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
現在ionicとLaravelをHTTP通信
|
1
|
+
現在ionicとLaravelを使用してHTTP通信を使おうとしているのですが、Laravel側のルートで
|
2
2
|
|
3
3
|
```Laravel
|
4
4
|
|
@@ -51,3 +51,181 @@
|
|
51
51
|
});
|
52
52
|
|
53
53
|
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
### 追記
|
58
|
+
|
59
|
+
ionicのリクエストを送っているコードです。
|
60
|
+
|
61
|
+
(現在ioonicでは[こちらの質問](https://teratail.com/questions/196148)にあるように422エラーが出ています。)
|
62
|
+
|
63
|
+
コードは[こちらの記事](https://blog.flicher.net/ionic-4-user-registration-login-tutorial/)と[こちらの記事](https://blog.flicher.net/laravel-rest-api-passport-authentication-for-ionic-app/)を参考に作成しました。
|
64
|
+
|
65
|
+
```TypeScript
|
66
|
+
|
67
|
+
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
68
|
+
|
69
|
+
import { Injectable } from '@angular/core';
|
70
|
+
|
71
|
+
import { tap } from 'rxjs/operators';
|
72
|
+
|
73
|
+
import { NativeStorage } from '@ionic-native/native-storage/ngx';
|
74
|
+
|
75
|
+
import { EnvService } from './env.service';
|
76
|
+
|
77
|
+
import { User } from '../models/user';
|
78
|
+
|
79
|
+
@Injectable({
|
80
|
+
|
81
|
+
providedIn: 'root'
|
82
|
+
|
83
|
+
})
|
84
|
+
|
85
|
+
export class AuthService {
|
86
|
+
|
87
|
+
isLoggedIn = false;
|
88
|
+
|
89
|
+
token:any;
|
90
|
+
|
91
|
+
constructor(
|
92
|
+
|
93
|
+
private http: HttpClient,
|
94
|
+
|
95
|
+
private storage: NativeStorage,
|
96
|
+
|
97
|
+
private env: EnvService,
|
98
|
+
|
99
|
+
) { }
|
100
|
+
|
101
|
+
login(email: String, password: String) {
|
102
|
+
|
103
|
+
return this.http.post(this.env.API_URL + 'auth/login',
|
104
|
+
|
105
|
+
{email: email, password: password}
|
106
|
+
|
107
|
+
).pipe(
|
108
|
+
|
109
|
+
tap(token => {
|
110
|
+
|
111
|
+
this.storage.setItem('token', token)
|
112
|
+
|
113
|
+
.then(
|
114
|
+
|
115
|
+
() => {
|
116
|
+
|
117
|
+
console.log('Token Stored');
|
118
|
+
|
119
|
+
},
|
120
|
+
|
121
|
+
error => console.error('Error storing item', error)
|
122
|
+
|
123
|
+
);
|
124
|
+
|
125
|
+
this.token = token;
|
126
|
+
|
127
|
+
this.isLoggedIn = true;
|
128
|
+
|
129
|
+
return token;
|
130
|
+
|
131
|
+
}),
|
132
|
+
|
133
|
+
);
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
register(fName: String, lName: String, email: String, password: String) {
|
138
|
+
|
139
|
+
return this.http.post(this.env.API_URL + 'auth/register',
|
140
|
+
|
141
|
+
{fName: fName, lName: lName, email: email, password: password}
|
142
|
+
|
143
|
+
)
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
logout() {
|
148
|
+
|
149
|
+
const headers = new HttpHeaders({
|
150
|
+
|
151
|
+
'Authorization': this.token["token_type"]+" "+this.token["access_token"]
|
152
|
+
|
153
|
+
});
|
154
|
+
|
155
|
+
return this.http.get(this.env.API_URL + 'auth/logout', { headers: headers })
|
156
|
+
|
157
|
+
.pipe(
|
158
|
+
|
159
|
+
tap(data => {
|
160
|
+
|
161
|
+
this.storage.remove("token");
|
162
|
+
|
163
|
+
this.isLoggedIn = false;
|
164
|
+
|
165
|
+
delete this.token;
|
166
|
+
|
167
|
+
return data;
|
168
|
+
|
169
|
+
})
|
170
|
+
|
171
|
+
)
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
user() {
|
176
|
+
|
177
|
+
const headers = new HttpHeaders({
|
178
|
+
|
179
|
+
'Authorization': this.token["token_type"]+" "+this.token["access_token"]
|
180
|
+
|
181
|
+
});
|
182
|
+
|
183
|
+
return this.http.get<User>(this.env.API_URL + 'auth/user', { headers: headers })
|
184
|
+
|
185
|
+
.pipe(
|
186
|
+
|
187
|
+
tap(user => {
|
188
|
+
|
189
|
+
return user;
|
190
|
+
|
191
|
+
})
|
192
|
+
|
193
|
+
)
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
getToken() {
|
198
|
+
|
199
|
+
return this.storage.getItem('token').then(
|
200
|
+
|
201
|
+
data => {
|
202
|
+
|
203
|
+
this.token = data;
|
204
|
+
|
205
|
+
if(this.token != null) {
|
206
|
+
|
207
|
+
this.isLoggedIn=true;
|
208
|
+
|
209
|
+
} else {
|
210
|
+
|
211
|
+
this.isLoggedIn=false;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
},
|
216
|
+
|
217
|
+
error => {
|
218
|
+
|
219
|
+
this.token = null;
|
220
|
+
|
221
|
+
this.isLoggedIn=false;
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
);
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
```
|
1
api.phpの内容の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,9 +42,9 @@
|
|
42
42
|
|
43
43
|
], function() {
|
44
44
|
|
45
|
-
Route::
|
45
|
+
Route::get('logout', 'Auth\AuthController@logout');
|
46
46
|
|
47
|
-
Route::
|
47
|
+
Route::get('user', 'Auth\AuthController@user');
|
48
48
|
|
49
49
|
});
|
50
50
|
|