-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
30 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,167 +4,30 @@ | |
|
||
use App\Http\Controllers\Backend\Auth\LoginController; | ||
use App\Http\Resources\UserSettingsResource; | ||
use App\Models\User; | ||
use App\Providers\AuthServiceProvider; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use App\Providers\AuthServiceProvider; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class AuthController extends Controller | ||
{ | ||
/** | ||
* @OA\Post( | ||
* path="/auth/signup", | ||
* operationId="registerUser", | ||
* tags={"Auth"}, | ||
* summary="register new user", | ||
* @OA\RequestBody( | ||
* required=true, | ||
* @OA\JsonContent( | ||
* @OA\Property( | ||
* property="username", | ||
* type="string", | ||
* minLength=3, | ||
* maxLength=25, | ||
* pattern="^[a-zA-Z0-9_]*$", | ||
* description="Username", | ||
* example="Gertrud123" | ||
* ), | ||
* @OA\Property ( | ||
* property="name", | ||
* type="string", | ||
* maxLength=50, | ||
* ), | ||
* @OA\Property ( | ||
* property="email", | ||
* example="[email protected]" | ||
* ), | ||
* @OA\Property( | ||
* property="password", | ||
* description="password", | ||
* type="string", | ||
* minLength=8, | ||
* maxLength=255, | ||
* example="thisisnotasecurepassword123" | ||
* ), | ||
* @OA\Property ( | ||
* property="password_confirmation", | ||
* description="confirmation of the password-field.", | ||
* type="string", | ||
* minLength=8, | ||
* maxLength=255, | ||
* example="thisisnotasecurepassword123" | ||
* ) | ||
* ) | ||
* ), | ||
* @OA\Response( | ||
* response=200, | ||
* description="successful operation", | ||
* @OA\JsonContent( | ||
* @OA\Property(property="data", type="object", | ||
* ref="#/components/schemas/BearerTokenResponse" | ||
* ) | ||
* ) | ||
* ), | ||
* @OA\Response(response=401, description="Other (not specified) error occured"), | ||
* @OA\Response(response=422, description="Username or email is already taken, or other input error") | ||
* ) | ||
* | ||
* | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
* @throws ValidationException | ||
* @api v1 | ||
*/ | ||
public function register(Request $request): JsonResponse { | ||
$validator = Validator::make($request->all(), [ | ||
'username' => ['required', 'unique:users', 'min:3', 'max:25', 'regex:/^[a-zA-Z0-9_]*$/'], | ||
'name' => ['required', 'max:50'], | ||
'email' => ['required', 'email', 'unique:users', 'max:255'], | ||
'password' => ['required', 'confirmed', 'max:255'], | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return response()->json([ | ||
'status' => 'error', | ||
'errors' => $validator->errors() | ||
], 422); | ||
} | ||
|
||
$validated = $validator->validated(); | ||
$validated['password'] = Hash::make($validated['password']); | ||
$validated['last_login'] = now(); | ||
$user = User::create($validated); | ||
|
||
if ($user->wasRecentlyCreated) { | ||
$userToken = $user->createToken('token', array_keys(AuthServiceProvider::$scopes)); | ||
return $this->sendResponse( | ||
data: [ | ||
'token' => $userToken->accessToken, | ||
'expires_at' => $userToken->token->expires_at->toIso8601String() | ||
], | ||
code: 201 | ||
); | ||
} | ||
return $this->sendError("Sorry! Registration is not successful.", 401); | ||
} | ||
|
||
/** | ||
* @OA\Post( | ||
* path="/auth/login", | ||
* operationId="loginUser", | ||
* tags={"Auth"}, | ||
* summary="Login with username & password", | ||
* @OA\RequestBody( | ||
* required=true, | ||
* @OA\JsonContent( | ||
* @OA\Property( | ||
* property="login", | ||
* type="string", | ||
* minLength=8, | ||
* maxLength=255, | ||
* description="Username or email", | ||
* example="[email protected]" | ||
* ), | ||
* @OA\Property( | ||
* property="password", | ||
* description="password", | ||
* type="string", | ||
* minLength=8, | ||
* maxLength=255, | ||
* example="thisisnotasecurepassword123" | ||
* ) | ||
* ) | ||
* ), | ||
* @OA\Response( | ||
* response=200, | ||
* description="successful operation", | ||
* @OA\JsonContent( | ||
* @OA\Property(property="data", type="object", | ||
* ref="#/components/schemas/BearerTokenResponse" | ||
* ) | ||
* ) | ||
* ), | ||
* @OA\Response(response=400, description="Bad request"), | ||
* @OA\Response(response=401, description="Non-matching credentials") | ||
* ) | ||
* | ||
* | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
* @api v1 | ||
* @deprecated Remove before 2023-10! Maybe earlier - if possible. Deprecation is already announced since | ||
* November'22. | ||
*/ | ||
public function login(Request $request): JsonResponse { | ||
$validated = $request->validate(['login' => ['required', 'max:255'], 'password' => ['required', 'min:8', 'max:255']]); | ||
|
||
if (LoginController::login($validated['login'], $validated['password'])) { | ||
$token = $request->user()->createToken('token', array_keys(AuthServiceProvider::$scopes)); | ||
return $this->sendResponse(['token' => $token->accessToken, | ||
'expires_at' => $token->token->expires_at->toIso8601String()]) | ||
return $this->sendResponse([ | ||
'WARNING' => 'This endpoint (login) is deprecated and will be removed in the following weeks. Please migrate to use OAuth2. More information: https://github.com/Traewelling/traewelling/issues/1772', | ||
'token' => $token->accessToken, | ||
'expires_at' => $token->token->expires_at->toIso8601String(), | ||
]) | ||
->header('Authorization', $token->accessToken); | ||
} | ||
return $this->sendError('Non-matching credentials', 401); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,137 +23,6 @@ | |
} | ||
], | ||
"paths": { | ||
"/auth/signup": { | ||
"post": { | ||
"tags": [ | ||
"Auth" | ||
], | ||
"summary": "register new user", | ||
"operationId": "registerUser", | ||
"requestBody": { | ||
"required": true, | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"properties": { | ||
"username": { | ||
"description": "Username", | ||
"type": "string", | ||
"maxLength": 25, | ||
"minLength": 3, | ||
"pattern": "^[a-zA-Z0-9_]*$", | ||
"example": "Gertrud123" | ||
}, | ||
"name": { | ||
"type": "string", | ||
"maxLength": 50 | ||
}, | ||
"email": { | ||
"example": "[email protected]" | ||
}, | ||
"password": { | ||
"description": "password", | ||
"type": "string", | ||
"maxLength": 255, | ||
"minLength": 8, | ||
"example": "thisisnotasecurepassword123" | ||
}, | ||
"password_confirmation": { | ||
"description": "confirmation of the password-field.", | ||
"type": "string", | ||
"maxLength": 255, | ||
"minLength": 8, | ||
"example": "thisisnotasecurepassword123" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "successful operation", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"properties": { | ||
"data": { | ||
"$ref": "#/components/schemas/BearerTokenResponse" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} | ||
} | ||
}, | ||
"401": { | ||
"description": "Other (not specified) error occured" | ||
}, | ||
"422": { | ||
"description": "Username or email is already taken, or other input error" | ||
} | ||
} | ||
} | ||
}, | ||
"/auth/login": { | ||
"post": { | ||
"tags": [ | ||
"Auth" | ||
], | ||
"summary": "Login with username & password", | ||
"operationId": "loginUser", | ||
"requestBody": { | ||
"required": true, | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"properties": { | ||
"login": { | ||
"description": "Username or email", | ||
"type": "string", | ||
"maxLength": 255, | ||
"minLength": 8, | ||
"example": "[email protected]" | ||
}, | ||
"password": { | ||
"description": "password", | ||
"type": "string", | ||
"maxLength": 255, | ||
"minLength": 8, | ||
"example": "thisisnotasecurepassword123" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "successful operation", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"properties": { | ||
"data": { | ||
"$ref": "#/components/schemas/BearerTokenResponse" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "Bad request" | ||
}, | ||
"401": { | ||
"description": "Non-matching credentials" | ||
} | ||
} | ||
} | ||
}, | ||
"/auth/logout": { | ||
"post": { | ||
"tags": [ | ||
|
@@ -2007,9 +1876,6 @@ | |
"401": { | ||
"description": "Unauthorized" | ||
}, | ||
"404": { | ||
"description": "No statuses found" | ||
}, | ||
"403": { | ||
"description": "User not authorized to access this" | ||
} | ||
|
@@ -5757,9 +5623,9 @@ | |
"scheme": "https", | ||
"flows": { | ||
"authorizationCode": { | ||
"authorizationUrl": "http://localhost/oauth/authorize", | ||
"tokenUrl": "http://localhost/oauth/token", | ||
"refreshUrl": "http://localhost/auth/refresh", | ||
"authorizationUrl": "http://localhost:8000/oauth/authorize", | ||
"tokenUrl": "http://localhost:8000/oauth/token", | ||
"refreshUrl": "http://localhost:8000/auth/refresh", | ||
"scopes": { | ||
"read-statuses": "see all statuses", | ||
"read-notifications": "see your notifications", | ||
|
Oops, something went wrong.