public function AuthUser($token) { // Check UserToken try { $data = UserToken::where('token', $token)->get(); if ($data->count()) { $data = $data->first(); $created_at = $data->updated_at; $now = Carbon::now(); $diff = $created_at->diffInMinutes($now); if ($diff > \Config::get('api.token.timeout')) { return ['status' => false, 'code' => 403, 'message' => 'Token expired.']; } else { return true; } } else { return ['status' => false, 'code' => 403, 'message' => 'Invalid token']; } } catch (\Exception $e) { throw new \Exception('System error.', 500); } }
} catch (Exception $e) { $create = new \Beggiatom\Api\Models\UserToken(); $create->user_id = Auth::user()->id; $create->token = $token; $create->save(); $updated_at = $create->updated_at; } $updatedCopy = $updated_at->copy(); $deadline_at = $updatedCopy->addMinutes(Config::get('api.token.timeout')); return ['status' => true, 'code' => 200, 'token' => $token, 'created_at' => $updated_at->toDateTimeString(), 'deadline_at' => $deadline_at->toDateTimeString()]; } else { return ['status' => false, 'code' => 403, 'message' => 'Invalid email or password.']; } } }); Route::get('/{token}', function ($token) { $data = \Beggiatom\Api\Models\UserToken::where('token', $token)->get(); if ($data->count()) { $data = $data->first(); return ['status' => true, 'code' => 200, 'data' => ['id' => $data->user->id, 'name' => $data->user->name, 'email' => $data->user->email, 'created_at' => $data->created_at, 'updated_at' => $data->updated_at, 'deadline_at' => \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $data->updated_at)->addMinute(Config::get('api.token.timeout'))]]; } else { return ['status' => false, 'code' => 200, 'message' => 'User not found']; } }); Route::get('logout', function () { Auth::logout(); return ['status' => true, 'code' => 200, 'message' => 'User logged out']; }); }); }); });