Skip to content

from affan - Feature/core functions #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

L5_SWAGGER_CONST_HOST=http://laravel_test/api/
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
110 changes: 110 additions & 0 deletions app/Http/Controllers/Api/V1/Auth/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace App\Http\Controllers\Api\V1\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use JWTAuth;
use App\Models\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Exception;

class AuthController extends Controller
{
# Invalid Login Response #
public function invalidLogin()
{
return response()->json([
'status' => Response::HTTP_UNAUTHORIZED,
'message' => "Token Missmatch"
], Response::HTTP_UNAUTHORIZED);
}

# Issued Token Response #
protected function respondWithToken($token)
{
return response()->json([
'status' => Response::HTTP_OK,
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => JWTAuth::factory()->getTTL() * 300 # expired in 5 hours
], Response::HTTP_OK);
}

/**
* @OA\Post(
* path="/login",
* summary="Login",
* operationId="authLogin",
* tags={"Authentication"},
* @OA\RequestBody(
* required=true,
* description="Login",
* @OA\JsonContent(
* required={"username","password"},
* @OA\Property(property="username", type="string", format="text", example="admin"),
* @OA\Property(property="password", type="string", format="password", example="admin"),
* ),
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
* ),
* @OA\Response(
* response=400,
* description="Bad Request",
* ),
* @OA\Response(
* response=401,
* description="Unauthorized",
* ),
* @OA\Response(
* response=403,
* description="Forbidden"
* ),
* @OA\Response(
* response=500,
* description="Internal Server Error",
* )
* )
*/
public function login(Request $request)
{
try {
# Validation Rules #
$credentials = $request->only('username','password');
$validator = Validator::make($credentials, [
'username' => 'required|string|min:3|max:50',
'password' => 'required|string|min:3|max:100',
]);

# Cred Validation #
if ($validator->fails()) {
return response()->json(['error' => $validator->errors(), 'status' => Response::HTTP_BAD_REQUEST], Response::HTTP_BAD_REQUEST);
}

# Token Issued #
if ($token = JWTAuth::attempt($credentials)) {
return $this->respondWithToken($token);
}

return response()->json([
'status' => Response::HTTP_UNAUTHORIZED,
'message' => "user not found"
], Response::HTTP_UNAUTHORIZED);

} catch (Exception $e) {
return response()->json([
'status' => Response::HTTP_BAD_REQUEST,
'message' => $e->getMessage()
], Response::HTTP_BAD_REQUEST);
}

}

}
Loading