|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Auth; |
| 4 | + |
| 5 | +use App\User; |
| 6 | +use Validator; |
| 7 | +use Tymon\JWTAuth\Exceptions\JWTException; |
| 8 | +use Tymon\JWTAuth\Facades\JWTAuth; |
| 9 | +use App\Http\Controllers\Controller; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Illuminate\Foundation\Auth\ThrottlesLogins; |
| 12 | +use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; |
| 13 | + |
| 14 | +class AuthController extends Controller |
| 15 | +{ |
| 16 | + /* |
| 17 | + |-------------------------------------------------------------------------- |
| 18 | + | Registration & Login Controller |
| 19 | + |-------------------------------------------------------------------------- |
| 20 | + | |
| 21 | + | This controller handles the registration of new users, as well as the |
| 22 | + | authentication of existing users. By default, this controller uses |
| 23 | + | a simple trait to add these behaviors. Why don't you explore it? |
| 24 | + | |
| 25 | + */ |
| 26 | + |
| 27 | + use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
| 28 | + |
| 29 | + /** |
| 30 | + * Where to redirect users after login / registration. |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $redirectTo = '/'; |
| 35 | + |
| 36 | + /** |
| 37 | + * Create a new authentication controller instance. |
| 38 | + * |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + // public function __construct() |
| 42 | + // { |
| 43 | + // $this->middleware($this->guestMiddleware(), ['except' => 'logout']); |
| 44 | + // } |
| 45 | + public function index(){ |
| 46 | + try{ |
| 47 | + return User::all(); |
| 48 | + }catch(Exception $ex){ |
| 49 | + return $ex; |
| 50 | + } |
| 51 | + } |
| 52 | + public function show(){ |
| 53 | + try { |
| 54 | + $user = JWTAuth::parseToken()->ToUser(); |
| 55 | + if(! $user){ |
| 56 | + return $this->response->errorNotFound("user not found"); |
| 57 | + } |
| 58 | + } catch (Exception $ex) { |
| 59 | + return $this->response->error("something went wrong"); |
| 60 | + } |
| 61 | + |
| 62 | + return $this->response->array(compact('user'))->setStatusCode(200); |
| 63 | + } |
| 64 | + public function authenticate(Request $request){ |
| 65 | + $credentials= $request->only('email','password'); |
| 66 | + try{ |
| 67 | + if(!$token = JWTAuth::attempt($credentials)){ |
| 68 | + return $this->response->errorUnauthorized(); |
| 69 | + } |
| 70 | + }catch(JWTException $ex){ |
| 71 | + return $this->response->errorInternal(); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + return $this->response->array(compact('token'))->setStatusCode(200); |
| 76 | + } |
| 77 | + /** |
| 78 | + * Get a validator for an incoming registration request. |
| 79 | + * |
| 80 | + * @param array $data |
| 81 | + * @return \Illuminate\Contracts\Validation\Validator |
| 82 | + */ |
| 83 | + protected function validator(array $data) |
| 84 | + { |
| 85 | + return Validator::make($data, [ |
| 86 | + 'name' => 'required|max:255', |
| 87 | + 'email' => 'required|email|max:255|unique:users', |
| 88 | + 'password' => 'required|min:6|confirmed', |
| 89 | + ]); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create a new user instance after a valid registration. |
| 94 | + * |
| 95 | + * @param array $data |
| 96 | + * @return User |
| 97 | + */ |
| 98 | + protected function create(array $data) |
| 99 | + { |
| 100 | + return User::create([ |
| 101 | + 'name' => $data['name'], |
| 102 | + 'email' => $data['email'], |
| 103 | + 'password' => bcrypt($data['password']), |
| 104 | + ]); |
| 105 | + } |
| 106 | +} |
0 commit comments