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

質問編集履歴

5

一部修正

2019/08/15 20:21

投稿

DaisukeMori
DaisukeMori

スコア229

title CHANGED
File without changes
body CHANGED
@@ -210,7 +210,7 @@
210
210
  ```
211
211
 
212
212
  ```php
213
- /app/Http/Resources/User.php
213
+ //app/Http/Resources/User.php
214
214
 
215
215
  namespace App\Http\Resources;
216
216
 

4

一部修正

2019/08/15 20:21

投稿

DaisukeMori
DaisukeMori

スコア229

title CHANGED
File without changes
body CHANGED
@@ -163,7 +163,7 @@
163
163
  ```
164
164
 
165
165
  ```php
166
- /app/Http/Controllers/UserController.php
166
+ //app/Http/Controllers/UserController.php
167
167
  namespace App\Http\Controllers;
168
168
 
169
169
  use App\User;

3

一部文字修正

2019/08/15 20:20

投稿

DaisukeMori
DaisukeMori

スコア229

title CHANGED
File without changes
body CHANGED
@@ -124,7 +124,7 @@
124
124
  ```
125
125
 
126
126
  ```php
127
- /app/Http/Controllers/AuthController.php
127
+ //app/Http/Controllers/AuthController.php
128
128
  namespace App\Http\Controllers;
129
129
 
130
130
  class AuthController extends Controller

2

コード追加

2019/08/15 20:20

投稿

DaisukeMori
DaisukeMori

スコア229

title CHANGED
File without changes
body CHANGED
@@ -91,4 +91,195 @@
91
91
  },
92
92
  }
93
93
  </script>
94
+ ```
95
+
96
+ # Laravelコード
97
+
98
+ ```php
99
+ //app/User.php
100
+ namespace App;
101
+
102
+ use Tymon\JWTAuth\Contracts\JWTSubject;
103
+ use Illuminate\Notifications\Notifiable;
104
+ use Illuminate\Foundation\Auth\User as Authenticatable;
105
+
106
+ class User extends Authenticatable implements JWTSubject
107
+ {
108
+ use Notifiable;
109
+
110
+ protected $fillable = ['name', 'email', 'password'];
111
+ protected $guarded = ['id' ];
112
+ protected $hidden = ['remember_token',];
113
+
114
+ public function getJWTIdentifier()
115
+ {
116
+ return $this->getKey();
117
+ }
118
+
119
+ public function getJWTCustomClaims()
120
+ {
121
+ return [];
122
+ }
123
+ }
124
+ ```
125
+
126
+ ```php
127
+ /app/Http/Controllers/AuthController.php
128
+ namespace App\Http\Controllers;
129
+
130
+ class AuthController extends Controller
131
+ {
132
+ function login() {
133
+ $credentials = request(['email', 'password']);
134
+
135
+ if (!$token = auth('api')->attempt($credentials)) {
136
+ return response()->json(['error' => 'Unauthorized'], 401);
137
+ }
138
+
139
+ return $this->respondWithToken($token);
140
+ }
141
+
142
+ public function logout()
143
+ {
144
+ auth()->logout();
145
+ return response()->json(['message' => 'ログアウトしました。']);
146
+ }
147
+
148
+ public function me()
149
+ {
150
+ return response()->json(auth()->user());
151
+ }
152
+
153
+ protected function respondWithToken($token)
154
+ {
155
+ return response()->json([
156
+ 'access_token' => $token,
157
+ 'token_type' => 'bearer',
158
+ 'expires_in' => auth("api")->factory()->getTTL() * 60
159
+ ]);
160
+ }
161
+ }
162
+
163
+ ```
164
+
165
+ ```php
166
+ /app/Http/Controllers/UserController.php
167
+ namespace App\Http\Controllers;
168
+
169
+ use App\User;
170
+ use Illuminate\Http\Request;
171
+ use App\Http\Resources\User AS UserResource;
172
+
173
+ class UserController extends Controller
174
+ {
175
+ // 一覧表示
176
+ public function index() {
177
+ return UserResource::collection(User::all());
178
+ }
179
+
180
+ // 保存
181
+ public function store(Request $request) {
182
+ $user = new User;
183
+ $user->name = $request->input('name');
184
+ $user->email = $request->input('email');
185
+ // bcrypt関数でハッシュ化してくれる
186
+ $user->password = bcrypt($request->input('password'));
187
+
188
+ $user->save();
189
+ }
190
+
191
+ // 1データ表示
192
+ public function show(User $user) {
193
+ return new UserResource($user);
194
+ }
195
+
196
+ // 更新
197
+ public function update(Request $request, User $user) {
198
+ $user->name = $request->input('name');
199
+ $user->email = $request->input('email');
200
+ // bcrypt関数でハッシュ化してくれる
201
+ $user->password = bcrypt($request->input('password'));
202
+ $user->save();
203
+ }
204
+
205
+ // 削除
206
+ public function destroy(User $user) {
207
+ $user->delete();
208
+ }
209
+ }
210
+ ```
211
+
212
+ ```php
213
+ /app/Http/Resources/User.php
214
+
215
+ namespace App\Http\Resources;
216
+
217
+ use Illuminate\Http\Resources\Json\Resource;
218
+
219
+ class User extends Resource
220
+ {
221
+ public function toArray($request)
222
+ {
223
+ return [
224
+ 'id' => $this->id,
225
+ 'name' => $this->name,
226
+ 'email' => $this->email,
227
+ 'password' => $this->password,
228
+ 'date' => $this->created_at->format('Y-m-d H:i:s'),
229
+ ];
230
+ }
231
+ }
232
+ ```
233
+
234
+ ```php
235
+ //routes/api.php
236
+
237
+ use Illuminate\Http\Request;
238
+
239
+ Route::middleware('auth:api')->get('/user', function (Request $request) {
240
+ return $request->user();
241
+ });
242
+
243
+ Route::post('/login', 'AuthController@login');
244
+
245
+ Route::group(['middleware' => 'auth:api'], function () {
246
+ Route::get('/me', 'AuthController@me');
247
+ Route::post('/logout', 'AuthController@logout');
248
+ });
249
+
250
+ Route::resource('users', 'UserController');
251
+ ```
252
+
253
+ ```php
254
+ //routes/web.php
255
+
256
+ Route::get('/home', 'HomeController@index')->name('home');
257
+
258
+ Route::any('{all}', function () {
259
+ return view('app');
260
+ })->where(['all' => '.*']);
261
+ ```
262
+
263
+ ```php
264
+ //resources/views/app.blade.php
265
+
266
+ <!doctype html>
267
+ <html lang="ja">
268
+ <head>
269
+ <meta charset="utf-8">
270
+ <title>Job+</title>
271
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
272
+ <link href="{{ mix('/css/app.css') }}" rel="stylesheet">
273
+ <link href="{{ asset('/css/clearfix.css') }}" rel="stylesheet">
274
+ <link href="{{ asset('/css/style.css') }}" rel="stylesheet">
275
+ <meta name="csrf-token" content="{{ csrf_token() }}">
276
+ </head>
277
+ <body>
278
+ <div id="app">
279
+ <app></app>
280
+ <router-view></router-view>
281
+ </div>
282
+ <script src="{{ mix('/js/app.js') }}"></script>
283
+ </body>
284
+ </html>
94
285
  ```

1

誤字修正

2019/08/15 20:14

投稿

DaisukeMori
DaisukeMori

スコア229

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  フロントエンドサイド
6
6
  Vue
7
- ログインにはjtw-auth使用 [参考](https://www.webopixel.net/php/1444.html)
7
+ ログインにはjwt-auth使用 [参考](https://www.webopixel.net/php/1444.html)
8
8
 
9
9
  # DB
10
10
  Laravelの標準のユーザーテーブル