示例#1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $user = new User();
     $user->name = 'Wahid';
     $user->email = '*****@*****.**';
     $user->password = bcrypt('password');
     $user->save();
     // create an admin
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\StoreNewRequest  $request
  * @return \Illuminate\Http\Response
  */
 public function store(StoreNewUserRequest $request)
 {
     // Grab the data that we want
     $data = $request->only('email', 'first_name', 'password');
     // create a new user
     $user = User::create(['name' => $data['first_name'], 'email' => $data['email'], 'password' => bcrypt($data['password'])]);
     return response()->api($user);
 }
示例#3
0
 /**
  * authenticate, handles the authentication for the initial request to login.
  * 
  * @param  Request  $request    The login request to handle the authentication
  * 
  * @return Response json        The data that came back
  */
 public function authenticate(Request $request)
 {
     $credentials = $request->only('email', 'password');
     $user = User::whereEmail($credentials['email'])->first();
     $adminClaim = ['admin' => false];
     // see if we have an admin user on our hands
     if ($user != null) {
         $adminClaim = ['admin' => $user->hasRole('admin')];
     }
     try {
         // attempt to verify the credentials and create a token for the user
         if (!($token = JWTAuth::attempt($credentials, $adminClaim))) {
             return response()->json(['error' => 'invalid_credentials'], 401);
         }
     } catch (JWTException $e) {
         // something went wrong whilst attempting to encode the token
         return response()->json(['error' => 'could_not_create_token'], 500);
     }
     return response()->json(compact('token'));
 }