-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #947 from NFDI4Chem/development
Development
- Loading branch information
Showing
79 changed files
with
9,592 additions
and
8,199 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
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
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
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 |
---|---|---|
|
@@ -10,8 +10,39 @@ | |
|
||
class LoginController extends Controller | ||
{ | ||
/** | ||
* @OA\Post( | ||
* path="/api/auth/login", | ||
* summary="Sign in", | ||
* description="Login by email and password", | ||
* operationId="authLogin", | ||
* tags={"auth"}, | ||
* | ||
* @OA\RequestBody( | ||
* required=true, | ||
* description="Pass user credentials", | ||
* | ||
* @OA\JsonContent( | ||
* required={"email","password"}, | ||
* | ||
* @OA\Property(property="email", type="string", format="email", example="[email protected]"), | ||
* @OA\Property(property="password", type="string", format="password", example="secret1234"), | ||
* ), | ||
* ), | ||
* | ||
* @OA\Response( | ||
* response=200, | ||
* description="Successful Operation", | ||
* ), | ||
* @OA\Response( | ||
* response=401, | ||
* description="Wrong Credentials Response", | ||
* ), | ||
* ) | ||
*/ | ||
public function login(Request $request): JsonResponse | ||
{ | ||
|
||
if (! Auth::attempt($request->only('email', 'password'))) { | ||
return response()->json([ | ||
'message' => 'Invalid login details', | ||
|
@@ -20,6 +51,12 @@ public function login(Request $request): JsonResponse | |
|
||
$user = User::where('email', $request['email'])->firstOrFail(); | ||
|
||
if (! $user->hasVerifiedEmail()) { | ||
return response()->json([ | ||
'message' => 'Account is not yet verified. Please verify your email address by clicking on the link we just emailed to you.', | ||
], 403); | ||
} | ||
|
||
$token = $user->createToken('auth_token')->plainTextToken; | ||
|
||
return response()->json([ | ||
|
@@ -28,6 +65,19 @@ public function login(Request $request): JsonResponse | |
]); | ||
} | ||
|
||
/** | ||
* @OA\Get( | ||
* path="/api/auth/logout", | ||
* summary="Sign out", | ||
* tags={"auth"}, | ||
* security={{"sanctum":{}}}, | ||
* | ||
* @OA\Response( | ||
* response=200, | ||
* description="successful operation" | ||
* ), | ||
* ) | ||
*/ | ||
public function logout(Request $request): JsonResponse | ||
{ | ||
$request->user()->currentAccessToken()->delete(); | ||
|
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 |
---|---|---|
|
@@ -3,36 +3,117 @@ | |
namespace App\Http\Controllers\API\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Team; | ||
use App\Models\User; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class RegisterController extends Controller | ||
{ | ||
public function register(Request $request): JsonResponse | ||
/** | ||
* Register | ||
* | ||
* @OA\Post ( | ||
* path="/api/auth/register", | ||
* tags={"auth"}, | ||
* | ||
* @OA\RequestBody( | ||
* | ||
* @OA\MediaType( | ||
* mediaType="application/json", | ||
* | ||
* @OA\Schema( | ||
* required={"first_name","last_name","email","password","username"}, | ||
* | ||
* @OA\Property(property="first_name", type="string", format="first_name", example="Nisha"), | ||
* @OA\Property(property="last_name", type="string", format="last_name", example="Sharma"), | ||
* @OA\Property(property="email", type="string", format="email", example="[email protected]"), | ||
* @OA\Property(property="username", type="string", format="username", example="nis123"), | ||
* @OA\Property(property="orcid_id", type="string", format="orcid_id", example="0009-0006-4755-1039"), | ||
* @OA\Property(property="password", type="string", format="password", example="secret1234"), | ||
* ) | ||
* ) | ||
* ), | ||
* | ||
* @OA\Response( | ||
* response=201, | ||
* description="Success", | ||
* | ||
* @OA\JsonContent( | ||
* | ||
* @OA\Property(property="success", type="boolean", example=true), | ||
* @OA\Property(property="message", type="string", example="User creation successful. Kindly confirm your email address by clicking the link sent to your inbox"), | ||
* @OA\Property(property="access_token", type="string", example="randomtokenasfhajskfhajf398rureuuhfdshk"), | ||
* @OA\Property(property="token_type", type="string", example="bearer"), | ||
* ) | ||
* ), | ||
* | ||
* @OA\Response( | ||
* response=422, | ||
* description="Unprocessable Content" | ||
* ) | ||
* ) | ||
*/ | ||
public function register(Request $request) | ||
{ | ||
$validatedData = $request->validate([ | ||
'name' => 'required|string|max:255', | ||
'email' => 'required|string|email|max:255|unique:users', | ||
'password' => 'required|string|min:8', | ||
'orcid_id' => string, | ||
'affiliation' => string, | ||
]); | ||
|
||
$user = User::create([ | ||
'name' => $validatedData['name'], | ||
'email' => $validatedData['email'], | ||
'orcid_id' => $validatedData['orcid_id'], | ||
'affiliation' => $validatedData['affiliation'], | ||
'password' => Hash::make($validatedData['password']), | ||
]); | ||
$validateUser = Validator::make($request->all(), | ||
[ | ||
'first_name' => 'required|string|max:255', | ||
'last_name' => 'required|string|max:255', | ||
'email' => 'required|string|email|max:255|unique:users', | ||
'password' => 'required|string|min:8', | ||
'username' => 'required|string', | ||
]); | ||
|
||
if ($validateUser->fails()) { | ||
return response()->json([ | ||
'status' => false, | ||
'message' => 'validation error', | ||
'errors' => $validateUser->errors(), | ||
], 401); | ||
} | ||
|
||
$user = DB::transaction(function () use ($request) { | ||
return tap(User::create([ | ||
'name' => $request['first_name'].' '.$request['last_name'], | ||
'first_name' => $request['first_name'], | ||
'last_name' => $request['last_name'], | ||
'email' => $request['email'], | ||
'username' => $request['username'], | ||
'orcid_id' => $request['orcid_id'], | ||
'affiliation' => $request['affiliation'], | ||
'password' => Hash::make($request['password']), | ||
]), function (User $user) { | ||
$this->createTeam($user); | ||
$user->sendEmailVerificationNotification(); | ||
}); | ||
}); | ||
|
||
$token = $user->createToken('auth_token')->plainTextToken; | ||
|
||
return response()->json([ | ||
'success' => true, | ||
'message' => 'User creation successful. Kindly confirm your email address by clicking the link sent to your inbox.', | ||
'access_token' => $token, | ||
'token_type' => 'Bearer', | ||
]); | ||
], | ||
201); | ||
} | ||
|
||
/** | ||
* Create a personal team for the user. | ||
* | ||
* @return void | ||
*/ | ||
protected function createTeam(User $user) | ||
{ | ||
$user->ownedTeams()->save(Team::forceCreate([ | ||
'user_id' => $user->id, | ||
'name' => explode(' ', $user->first_name.' '.$user->last_name, 2)[0]."'s Team", | ||
'personal_team' => true, | ||
])); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Auth; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Auth\Events\Verified; | ||
use Illuminate\Http\Request; | ||
|
||
class VerificationController extends Controller | ||
{ | ||
public function verify($user_id, Request $request) | ||
{ | ||
if (! $request->hasValidSignature()) { | ||
return response()->json(['msg' => 'Invalid/Expired url provided.'], 401); | ||
} | ||
|
||
$user = User::findOrFail($user_id); | ||
|
||
if (! hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) { | ||
throw new AuthorizationException; | ||
} | ||
|
||
if ($request->user() && $request->user()->getKey() != $user_id) { | ||
Auth::logout(); | ||
throw new AuthorizationException; | ||
} | ||
|
||
if (! $user->hasVerifiedEmail()) { | ||
if ($user->markEmailAsVerified()) { | ||
event(new Verified($user)); | ||
} | ||
} | ||
|
||
return redirect()->route('welcome')->with('success', 'Email verification Successful'); | ||
} | ||
|
||
/** | ||
* @OA\Get( | ||
* path="/api/auth/email/resend", | ||
* summary="Resend verification email", | ||
* tags={"auth"}, | ||
* security={{"sanctum":{}}}, | ||
* | ||
* @OA\Response( | ||
* response=200, | ||
* description="successful operation" | ||
* ), | ||
* ) | ||
*/ | ||
public function resend() | ||
{ | ||
if (auth()->user()) { | ||
// if (auth()->user()->hasVerifiedEmail()) { | ||
// return response()->json(['msg' => 'Email already verified.'], 400); | ||
// } | ||
|
||
auth()->user()->sendEmailVerificationNotification(); | ||
|
||
return response()->json(['msg' => 'Email verification link sent on your email id']); | ||
} | ||
} | ||
} |
Oops, something went wrong.