teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

リクエスト側のコードの追加、引用先の追加

2019/06/22 05:34

投稿

dauto
dauto

スコア38

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 現在ionicとLaravelをHTTP通信しようとしているのですが、Laravel側のルートで
1
+ 現在ionicとLaravelを使用してHTTP通信を使おうとしているのですが、Laravel側のルートで
2
2
  ```Laravel
3
3
  Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
4
4
  The GET method is not supported for this route. Supported methods: POST.
@@ -24,4 +24,93 @@
24
24
  Route::get('user', 'Auth\AuthController@user');
25
25
  });
26
26
  });
27
+ ```
28
+
29
+ ### 追記
30
+ ionicのリクエストを送っているコードです。
31
+ (現在ioonicでは[こちらの質問](https://teratail.com/questions/196148)にあるように422エラーが出ています。)
32
+ コードは[こちらの記事](https://blog.flicher.net/ionic-4-user-registration-login-tutorial/)と[こちらの記事](https://blog.flicher.net/laravel-rest-api-passport-authentication-for-ionic-app/)を参考に作成しました。
33
+ ```TypeScript
34
+ import { HttpClient, HttpHeaders } from '@angular/common/http';
35
+ import { Injectable } from '@angular/core';
36
+ import { tap } from 'rxjs/operators';
37
+ import { NativeStorage } from '@ionic-native/native-storage/ngx';
38
+ import { EnvService } from './env.service';
39
+ import { User } from '../models/user';
40
+ @Injectable({
41
+ providedIn: 'root'
42
+ })
43
+ export class AuthService {
44
+ isLoggedIn = false;
45
+ token:any;
46
+ constructor(
47
+ private http: HttpClient,
48
+ private storage: NativeStorage,
49
+ private env: EnvService,
50
+ ) { }
51
+ login(email: String, password: String) {
52
+ return this.http.post(this.env.API_URL + 'auth/login',
53
+ {email: email, password: password}
54
+ ).pipe(
55
+ tap(token => {
56
+ this.storage.setItem('token', token)
57
+ .then(
58
+ () => {
59
+ console.log('Token Stored');
60
+ },
61
+ error => console.error('Error storing item', error)
62
+ );
63
+ this.token = token;
64
+ this.isLoggedIn = true;
65
+ return token;
66
+ }),
67
+ );
68
+ }
69
+ register(fName: String, lName: String, email: String, password: String) {
70
+ return this.http.post(this.env.API_URL + 'auth/register',
71
+ {fName: fName, lName: lName, email: email, password: password}
72
+ )
73
+ }
74
+ logout() {
75
+ const headers = new HttpHeaders({
76
+ 'Authorization': this.token["token_type"]+" "+this.token["access_token"]
77
+ });
78
+ return this.http.get(this.env.API_URL + 'auth/logout', { headers: headers })
79
+ .pipe(
80
+ tap(data => {
81
+ this.storage.remove("token");
82
+ this.isLoggedIn = false;
83
+ delete this.token;
84
+ return data;
85
+ })
86
+ )
87
+ }
88
+ user() {
89
+ const headers = new HttpHeaders({
90
+ 'Authorization': this.token["token_type"]+" "+this.token["access_token"]
91
+ });
92
+ return this.http.get<User>(this.env.API_URL + 'auth/user', { headers: headers })
93
+ .pipe(
94
+ tap(user => {
95
+ return user;
96
+ })
97
+ )
98
+ }
99
+ getToken() {
100
+ return this.storage.getItem('token').then(
101
+ data => {
102
+ this.token = data;
103
+ if(this.token != null) {
104
+ this.isLoggedIn=true;
105
+ } else {
106
+ this.isLoggedIn=false;
107
+ }
108
+ },
109
+ error => {
110
+ this.token = null;
111
+ this.isLoggedIn=false;
112
+ }
113
+ );
114
+ }
115
+ }
27
116
  ```

1

api.phpの内容の修正

2019/06/22 05:33

投稿

dauto
dauto

スコア38

title CHANGED
File without changes
body CHANGED
@@ -20,8 +20,8 @@
20
20
  Route::group([
21
21
  'middleware' => 'auth:api'
22
22
  ], function() {
23
- Route::post('logout', 'Auth\AuthController@logout');
23
+ Route::get('logout', 'Auth\AuthController@logout');
24
- Route::post('user', 'Auth\AuthController@user');
24
+ Route::get('user', 'Auth\AuthController@user');
25
25
  });
26
26
  });
27
27
  ```