/** * Authenticate Routes * */ public function authenticate() { $app = Slim::getInstance(); $token = $app->request->headers->get('Authorization'); $response = $app->response(); $response->header("Content-Type", "application/json"); if (!isset($token)) { return Auth::deny_access("Authorization Token is not set. Please login"); } //Get user by token; try { $token = htmlentities(trim($token)); $user = User::findByToken($token); if ($user->token_expire < date('Y-m-d H:i:s')) { return Auth::deny_access("Authorization Token has expired. Please login again."); } $user->token_expire = date('Y-m-d H:i:s', strtotime('+1 hour')); $user->save(); } catch (ModelNotFoundException $e) { return Auth::deny_access("Authorization Token is invalid."); } }
public function login() { $app = Slim::getInstance(); $response = $app->response(); $response->header("Content-Type", "application/json"); $username = $app->request()->params('username'); $password = $app->request()->params('password'); if (!isset($username)) { return Auth::deny_access("Username is null"); } if (!isset($password)) { return Auth::deny_access("Password is null"); } try { $username = htmlentities(trim($username)); $password = htmlentities(trim($password)); $user = User::where('username', $username); if ($user->password != $password) { return Auth::deny_access("Incorrect Authentication Details"); } $responseArray['username'] = $user->username; $responseArray['token'] = bin2hex(openssl_random_pseudo_bytes(16)); //generate a random token $tokenExpiration = date('Y-m-d H:i:s', strtotime('+1 hour')); //the expiration date will be in one hour from the current moment $updatedUser = $user; $updatedUser->token = $responseArray['token']; $updatedUser->token_expire = $tokenExpiration; $updatedUser->save(); //Save the token and token expiration date for the user $response->status(200); $response->body(json_encode($responseArray)); } catch (ModelNotFoundException $e) { $response = Auth::deny_access("Incorrect Authentication Details"); } return $response; }
public function addEmoji() { $app = Slim::getInstance(); $request = $app->request(); $token = $request->headers->get('Authorization'); $name = $request->params('name'); $emoji_char = $request->params('char'); $keywords = $request->params('keywords'); $category = $request->params('category'); if (!isset($name)) { return Auth::deny_access("Emoji name is null"); } if (!isset($emoji_char)) { return Auth::deny_access("Emoji character value is null"); } if (!isset($category)) { return Auth::deny_access("Emoji category is null"); } if (!isset($keywords)) { return Auth::deny_access("Emoji keywords is null"); } $response = $app->response(); $response->header("Content-Type", "application/json"); $emoji = new Emoji(); $emoji->name = $name; $emoji->emoji_char = $emoji_char; $emoji->category = $category; $emoji->keywords = $keywords; $emoji->date_created = date('Y-m-d H:i:s'); $emoji->date_modified = date('Y-m-d H:i:s'); $emoji->created_by = User::findByToken($token)->id; $emoji->save(); $responseArray['message'] = "Emoji has been successfully created"; $response->status(200); $response->body(json_encode($responseArray)); return $response; }