Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/IT-Academy-BCN/ita-landing
Browse files Browse the repository at this point in the history
… into feature/register
  • Loading branch information
Lauddar committed May 25, 2023
2 parents 9280de1 + c9c40d0 commit 451a1e4
Show file tree
Hide file tree
Showing 9 changed files with 1,535 additions and 240 deletions.
46 changes: 46 additions & 0 deletions app/Http/Controllers/api/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class AuthController extends Controller
{

/**
* Log in a user and create a session
*
* @method POST
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
// Validate inputs
$request->validate([
'dni' => 'required',
'password' => 'required|string'
]);

// Get user's credentials
$credentials = [
'dni' => $request->dni,
'password' => $request->password,
];

// Verify user credentials
if (Auth::attempt($credentials)) {
$user = Auth::user();
$token = $user->createToken('authToken')->accessToken;

return response()->json(['result' => ['message' => 'Logged in successfully!', 'access_token' => $token], 'status' => true]);
} else {
return response()->json(['result' => ['message' => 'Invalid credentials'], 'status' => false], 401);
}
}


}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^8.1",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.0",
"laravel/passport": "^11.8",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8"
},
Expand Down
Loading

0 comments on commit 451a1e4

Please sign in to comment.