22
33namespace Danilowa \LaravelApiAuth \Controllers ;
44
5- use Illuminate \Http \Request ;
65use Illuminate \Http \JsonResponse ;
76use Illuminate \Routing \Controller ;
87use Illuminate \Support \Facades \Auth ;
98use Illuminate \Support \Facades \Hash ;
109use Danilowa \LaravelApiAuth \DTOs \UserLoginData ;
10+ use Danilowa \LaravelApiAuth \DTOs \UserLogoutData ;
1111use Danilowa \LaravelApiAuth \DTOs \UserRegistrationData ;
1212use Danilowa \LaravelApiAuth \Services \AccessTokenService ;
13+ use Danilowa \LaravelResponseBuilder \JsonResponse as JsonResponseBuilder ;
1314
1415class AuthenticationController extends Controller
1516{
@@ -29,20 +30,23 @@ public function __construct(AccessTokenService $accessTokenService)
2930 /**
3031 * Register a new user and create an access token.
3132 *
32- * @param UserRegistrationData $request
33- * @return JsonResponse
33+ * This method handles the registration of a new user.
34+ * It validates the provided data, creates a new user in the database,
35+ * and generates an access token for the user.
36+ *
37+ * @param UserRegistrationData $request The user registration data transfer object.
38+ * @return JsonResponse A JSON response containing the newly created user and their access token.
3439 */
3540 public function register (UserRegistrationData $ request ): JsonResponse
3641 {
37- $ user = $ this ->userModel ::create ([
38- 'name ' => $ request ->name ,
39- 'email ' => $ request ->email ,
40- 'password ' => Hash::make ($ request ->password ),
41- ]);
42+ $ data = $ request ->only (array_keys (config ('apiauth.validation.registration.rules ' )));
43+ $ data ['password ' ] = Hash::make ($ request ->password );
44+
45+ $ user = $ this ->userModel ::create ($ data );
4246
4347 $ token = $ this ->accessTokenService ->createToken ($ user , $ request ->token_name ?? $ this ->defaultTokenName );
4448
45- return JsonResponse ::success ([
49+ return JsonResponseBuilder ::success ([
4650 'user ' => $ user ,
4751 'token ' => $ token ->plainTextToken ,
4852 ], $ this ->getMessage ('user_created ' ), 201 );
@@ -51,19 +55,22 @@ public function register(UserRegistrationData $request): JsonResponse
5155 /**
5256 * Log a user in and create an access token.
5357 *
54- * @param UserLoginData $request
55- * @return JsonResponse
58+ * This method attempts to authenticate a user using the provided credentials.
59+ * If successful, it generates an access token for the authenticated user.
60+ *
61+ * @param UserLoginData $request The user login data transfer object.
62+ * @return JsonResponse A JSON response containing the access token and the authenticated user.
5663 */
5764 public function login (UserLoginData $ request ): JsonResponse
5865 {
59- if (!Auth::attempt ($ request ->only (' email ' , ' password ' ))) {
60- return JsonResponse ::error (401 , $ this ->getMessage ('credentials_incorrect ' ));
66+ if (!Auth::attempt ($ request ->only (array_keys ( config ( ' apiauth.validation.login.rules ' )) ))) {
67+ return JsonResponseBuilder ::error (401 , $ this ->getMessage ('credentials_incorrect ' ));
6168 }
6269
6370 $ user = Auth::user ();
6471 $ token = $ this ->accessTokenService ->createToken ($ user , $ request ->token_name ?? $ this ->defaultTokenName );
6572
66- return JsonResponse ::success ([
73+ return JsonResponseBuilder ::success ([
6774 'token ' => $ token ->plainTextToken ,
6875 'user ' => $ user ,
6976 ], $ this ->getMessage ('user_logged_in ' ));
@@ -72,42 +79,55 @@ public function login(UserLoginData $request): JsonResponse
7279 /**
7380 * Log the user out, revoking their access tokens.
7481 *
75- * @param Request $request
76- * @return JsonResponse
82+ * This method allows a user to log out by revoking their access tokens.
83+ * It checks whether the user is authenticated and whether they have any active tokens.
84+ *
85+ * @param UserLogoutData $request The user logout data transfer object.
86+ * @return JsonResponse A JSON response indicating the outcome of the logout process.
7787 */
78- public function logout (Request $ request ): JsonResponse
88+ public function logout (UserLogoutData $ request ): JsonResponse
7989 {
90+ if (!Auth::attempt ($ request ->only ('email ' , 'password ' ))) {
91+ return JsonResponseBuilder::error (401 , $ this ->getMessage ('credentials_incorrect ' ));
92+ }
93+
94+ $ user = $ this ->userModel ::find ($ request ->user ()->id );
8095 $ revokeAll = config ('apiauth.revoke_all_tokens ' );
8196
82- if ($ revokeAll ) {
83- $ request ->user ()->tokens ()->delete ();
84- return JsonResponse::success ([], $ this ->getMessage ('tokens_revoked ' ));
97+ if ($ user ->tokens ()->count () === 0 ) {
98+ return JsonResponseBuilder::error (404 , $ this ->getMessage ('no_active_token ' ));
8599 }
86100
87- $ token = $ request -> user ()-> currentAccessToken ();
88- if (! $ token ) {
89- return JsonResponse:: error ( 404 , $ this ->getMessage ('no_active_token ' ));
101+ if ( $ revokeAll ) {
102+ $ user -> tokens ()-> delete ();
103+ return JsonResponseBuilder:: success ([] , $ this ->getMessage ('tokens_revoked ' ));
90104 }
91105
106+ $ token = $ user ->tokens ()->first ();
92107 $ this ->accessTokenService ->revokeToken ($ token );
93- return JsonResponse ::success ([], $ this ->getMessage ('tokens_revoked ' ));
108+ return JsonResponseBuilder ::success ([], $ this ->getMessage ('token_revoked ' ));
94109 }
95110
96111 /**
97112 * Get the currently authenticated user.
98113 *
99- * @return JsonResponse
114+ * This method retrieves the currently authenticated user's information.
115+ *
116+ * @return JsonResponse A JSON response containing the authenticated user's data.
100117 */
101118 public function currentUser (): JsonResponse
102119 {
103- return JsonResponse ::success (Auth::user ());
120+ return JsonResponseBuilder ::success (Auth::user ());
104121 }
105122
106123 /**
107124 * Retrieve a message from the configuration.
108125 *
109- * @param string $key
110- * @return string
126+ * This private method fetches a message from the configuration based on the provided key.
127+ * If the key does not exist, it returns a default message.
128+ *
129+ * @param string $key The key for the message in the configuration.
130+ * @return string The corresponding message or a default message if not found.
111131 */
112132 private function getMessage (string $ key ): string
113133 {
0 commit comments