public function __construct() { $this->app_list_limit = env('APP_LIST_LIMIT', 50); $token = JWTAuth::getToken(); if (!empty($token)) { $user = JWTAuth::toUser($token); $this->logged_user = User::find($user->id); } }
/** * Handle a registration request for the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postRegister(Request $request) { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwValidationException($request, $validator); } $token = JWTAuth::fromUser($this->create($request->all()), $this->customClaims()); $response = new JsonResponse(['token' => $token], 200); $response->header('Authorization', 'Bearer ' . $token); return $response; }
public function inValidateToken() { $tempStorage = app('\\App\\Http\\Controllers\\TEMPStorage\\UserTempStorage'); $tempStorage->forget('id_company'); //set model login $user = JWTAuth::parseToken()->authenticate(); $this->model->find($user->id)->update(['login' => 0]); JWTAuth::invalidate(JWTAuth::getToken()); return API::response()->array(['message' => 'success'])->statusCode(200); }
public function addPoint(Request $request) { $user = JWTAuth::parseToken()->authenticate(); $user->points = $user->points + $request->input('amount'); $user->save(); return response()->json(['success' => true, 'message' => "Users points added", 'users' => $user]); }
public function getUserFromCookie($cookie) { $tokenObject = new Token($cookie); // Get a payload info from the token try { $payload = JWTAuth::decode($tokenObject); } catch (TokenExpiredException $e) { $message = 'Token in cookie was expired'; throw new TokenInCookieExpiredException($message, null, $e); } // Get user by the payload info try { $user = $this->userUpdater->updateBaseInfo($payload); } catch (RepositoryException $e) { throw new AuthException($e->getMessage(), null, $e); } // Attempt to update his profile by API or just log the error try { $user = $this->userUpdater->updateAdditionalInfo($cookie, $user); } catch (UpdatingFailureException $e) { Log::warning('An additional user information was\'nt updated. ' . $e->getMessage()); } // Login Auth::login($user, true); // Return an actual user model if login passes if (Auth::check()) { return $this->userRepository->findWithRelations(Auth::id(), ['localRole']); } else { throw new AuthException('Login error. User is not authorized.'); } }
public function index(FacebookAuthentication $request, FacebookGraphClient $client, FacebookUserResolver $resolver) { $data = $client->init($request->token)->getUser(['id', 'email', 'first_name', 'last_name']); $user = $resolver->findOrCreateUser($data); $customClaims = ['name' => $user->name, 'email' => $user->email, 'role' => $user->role, 'gravatar' => $user->gravatar]; return api_response(200, ['token' => JWTAuth::fromUser($user, $customClaims)]); }
/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postLogin(Request $request) { $usernames = $this->loginUsername(); if (!is_array($usernames)) { $usernames = [$usernames]; } $usernamesR = []; foreach ($usernames as $username) { $usernamesR[$username] = 'required'; } $this->validate($request, array_merge($usernamesR, ['password' => 'required'])); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. $throttles = $this->isUsingThrottlesLoginsTrait(); if ($throttles && $this->hasTooManyLoginAttempts($request)) { return $this->sendLockoutResponse($request); } $credentials = $this->getCredentials($request); if ($token = JWTAuth::attempt($credentials, $this->customClaims())) { return $this->handleUserWasAuthenticated($request, $throttles, $token); } // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. if ($throttles) { $this->incrementLoginAttempts($request); } return new JsonResponse([implode('.', $usernames) => [$this->getFailedLoginMessage()]], 422); }
public function register(UserRequest $request) { $newUser = ['name' => $request->get('name'), 'email' => $request->get('email'), 'password' => bcrypt($request->get('password'))]; $user = User::create($newUser); $token = JWTAuth::fromUser($user); return response()->json(compact('token')); }
protected function me() { if ($token = JWTAuth::getToken()) { return JWTAuth::parseToken()->toUser(); } return false; }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next, $aplicacion) { $metodo = $request->method(); $user = JWTAuth::parseToken()->authenticate(); switch ($metodo) { case 'GET': $tipo_permiso = 1; break; case 'POST': $tipo_permiso = 2; break; case 'PUT': $tipo_permiso = 2; break; case 'DELETE': $tipo_permiso = 2; break; } $privilegios = Privilegio::with('aplicacion')->where('user_id', $user->id)->where('aplicacion_id', $aplicacion); if ($tipo_permiso == 1) { $privilegios = $privilegios->where(function ($query) { $query->where('privilegios_tipo_id', 1)->orWhere('privilegios_tipo_id', 2); }); } else { $privilegios = $privilegios->where('privilegios_tipo_id', $tipo_permiso); } $privilegios = $privilegios->first(); if ($privilegios) { return $next($request); } else { return response('Unauthorized.', 401); } }
/** * Permintaan refresh token * * @param Request $request * @return array */ public function refreshToken(Request $request) { $this->middleware('auth'); $user = app('auth')->user(); $newToken = JWTAuth::parseToken()->refresh(); return ['status' => 'success', 'user' => $user, 'token' => $newToken]; }
public function __construct(User $user, Project $project, Invitation $invitation) { $this->loggedUser = JWTAuth::parseToken()->authenticate(); $this->user = $user; $this->project = $project; $this->invitation = $invitation; }
public function signin() { $credentials = Input::only('email', 'password'); if (!($token = JWTAuth::attempt($credentials))) { return Response::json(false, HttpResponse::HTTP_UNAUTHORIZED); } return Response::json(compact('token')); }
/** * Display a listing of the resource. * * @return Response */ public function auth(Request $request) { $userData = $this->currentUser->payloadInfo; $payload = JWTFactory::make($userData); $data = JWTAuth::encode($payload); $redirectPath = $request->cookie('referer'); return Redirect::to($redirectPath, 303)->withCookie('x-access-token', $data->get())->withCookie('serverUID', $userData['id']); }
/** * Display a listing of the resource. * * @return Response */ public function auth(Request $request) { $customClaims = ['id' => '55dc13391846c68a1ad56daa', 'email' => 'admin@admin', 'role' => 'ADMIN', 'iat' => 1440615292]; $payload = JWTFactory::make($customClaims); $data = JWTAuth::encode($payload); $redirectPath = $request->cookie('referer'); return Redirect::to($redirectPath, 303)->withCookie('x-access-token', $data->get())->withCookie('serverUID', '55dc13391846c68a1ad56daa'); }
public function postLogin(Request $request) { $credentials = $request->only('email', 'password'); $token = JWTAuth::attempt($credentials); if (!$token) { return response()->json('Incorrect username or password combination.', Response::HTTP_UNAUTHORIZED); } return response()->json(compact('token')); }
public function postRegister(Request $request) { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwValidationException($request, $validator); } $token = JWTAuth::fromUser($this->create($request->all())); return response()->json(['token' => $token]); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param int $level * @return mixed * @throws \Bican\Roles\Exceptions\LevelDeniedException */ public function handle($request, Closure $next, $level) { if (!($user = JWTAuth::parseToken()->authenticate())) { return response()->json(['user_not_found'], 404); } if ($user->level() >= $level) { return $next($request); } throw new LevelDeniedException($level); }
public function destroy() { $user = JWTAuth::parseToken()->authenticate(); if ($user) { User::destroy($user->id); return response('Success'); } else { return response('Unathorized', 403); } }
public function getState() { $token = JWTAuth::getToken(); $user = JWTAuth::toUser($token); $unlocked = $user->devices()->unlocked(); $device_state = $unlocked->count() == 0 ? 1 : 0; $data = ['state' => $device_state, 'username' => $user->first_name]; $this->putStats($user); return Response::json($data); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (count(Enseignant_Privilege::where(function ($query) { $query->where('id_Enseignant', '=', JWTAuth::parseToken()->toUser()->id_Enseignant)->where('id_Privilege', '=', '7'); })->get()) > 0) { return $next($request); } else { return Response::json(['error' => 'Permission denied'], HttpResponse::HTTP_UNAUTHORIZED); } }
public function currentUserIsOwner() { $user = JWTAuth::parseToken()->authenticate(); $user_id = $user->id; if ($this->user_id == $user_id) { return true; } else { return false; } }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param int|string $permission * @return mixed * @throws \Sim\Roles\Exceptions\PermissionDeniedException */ public function handle($request, Closure $next, $permission) { if (!($user = JWTAuth::parseToken()->authenticate())) { return response()->json(['user_not_found'], 404); } if ($user->can($permission)) { return $next($request); } throw new PermissionDeniedException($permission); }
protected function checkUserIsLogged() { $user = JWTAuth::parseToken()->authenticate(); if (!$user) { return response()->json(["msg" => "You must be authenticated", "data" => false], 404); } else { $this->user = $user; return true; } }
/** * Return request headers needed to interact with the API. * * @return Array array of headers. */ protected function headers($user = null) { $headers = ['Accept' => 'application/json']; if (!is_null($user)) { $token = JWTAuth::fromUser($user); JWTAuth::setToken($token); $headers['Authorization'] = 'Bearer ' . $token; } return $headers; }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $user_created = $this->userRepository->create($request->all()); if ($user_created instanceof MessageBag) { return response()->json($user_created)->setStatusCode(412, 'Invalid User'); } $id = $user_created->id; $token = JWTAuth::fromUser($user_created); return response()->json(['id' => $id, 'uri' => url('profile', ['id' => $id])] + compact('token'))->setStatusCode(201); }
public function destroy($id) { $user = JWTAuth::parseToken()->authenticate(); $todo = Todo::where('owner_id', $user->id)->where('id', $id)->first(); if ($todo) { Todo::destroy($id); return response('Success'); } else { return response('Unathorized', 403); } }
/** * @param $cookie * * @return static */ public static function getUserByCookie($cookie) { $tokenObject = new Token($cookie); $payload = JWTAuth::decode($tokenObject); $userInfo = $payload->toArray(); // temp test user $user = User::firstOrCreate(['email' => $userInfo['email']]); $role = array_key_exists('role', $userInfo) ? $userInfo['role'] : "DEVELOPER"; $user->update(['bid' => $userInfo['id'], 'role' => $role, 'first_name' => $userInfo['email'], 'last_name' => '', 'phone' => '666-66-666', 'avatar' => 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($userInfo['email']))) . '?d=retro', 'address' => 'iat: ' . $userInfo['iat'], 'job_id' => 1, 'department_id' => 1]); return $user; }
public function postSignup(Request $request) { $credentials = $request->only('username', 'password'); try { $user = Ulibier::create($credentials); } catch (Exception $e) { return response()->json(['error' => 'User already exists.'], 409); } $token = JWTAuth::fromUser($user); return response()->json(compact('token')); }
/** * Handle the event. * * @param Login $event * @return void */ public function handle(Login $event) { $tokenId = base64_encode(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); $issuedAt = Carbon::now()->timestamp; $notBefore = $issuedAt; $expire = $notBefore + 6 * 60 * 60; // Adding 6 hours // Create the token $config = ['iat' => $issuedAt, 'jti' => $tokenId, 'iss' => config('app.url'), 'nbf' => $notBefore, 'exp' => $expire, 'data' => ['userId' => $event->user->id]]; Session::put('jwt', JWTAuth::fromUser($event->user, $config)); }