SHARE
TWEET

Untitled

a guest Mar 26th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\User;
  6. use Illuminate\Support\Facades\Auth;
  7. use Validator;
  8.  
  9. class UserController extends Controller
  10. {
  11.     public $successStatus = 200;
  12.     /**
  13.      * login api
  14.      *
  15.      * @return \Illuminate\Http\Response
  16.      */
  17.     public function login(){
  18.         if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
  19.             $user = Auth::user();
  20.             $success['token'] =  $user->createToken('MyApp')-> accessToken;
  21.             return response()->json(['success' => $success], $this-> successStatus);
  22.         }
  23.         else{
  24.             return response()->json(['error'=>'Unauthorised'], 401);
  25.         }
  26.     }
  27.     /**
  28.      * Register api
  29.      *
  30.      * @return \Illuminate\Http\Response
  31.      */
  32.     public function register(Request $request)
  33.     {
  34.         $validator = Validator::make($request->all(), [
  35.             'username' => 'required',
  36.             'firstname' => 'required',
  37.             'name' => 'required',
  38.             'age' => 'required',
  39.             'email' => 'required|email',
  40.             'password' => 'required',
  41.             'c_password' => 'required|same:password',
  42.         ]);
  43.         if ($validator->fails()) {
  44.             return response()->json(['error'=>$validator->errors()], 401);
  45.         }
  46.         $input = $request->all();
  47.         $input['password'] = bcrypt($input['password']);
  48.         $user = User::create($input);
  49.         $success['token'] =  $user->createToken('MyApp')-> accessToken;
  50.         $success['username'] =  $user->username;
  51.         $success['firstname'] =  $user->firstname;
  52.         $success['name'] =  $user->name;
  53.         $success['age'] =  $user->age;
  54.         return response()->json(['success'=>$success], $this-> successStatus);
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top