2FAuth/app/Http/Controllers/UserController.php

162 lines
4.0 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
2019-05-20 07:37:41 +02:00
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
2020-01-23 23:02:54 +01:00
use Illuminate\Support\Facades\Hash;
2019-05-20 07:37:41 +02:00
class UserController extends Controller
{
2019-05-29 11:04:12 +02:00
/**
* log a user in
* @return [type] [description]
*/
2020-01-09 11:14:39 +01:00
public function login(Request $request)
2019-05-20 07:37:41 +02:00
{
$this->validate($request, [
2020-01-09 11:14:39 +01:00
'email' => 'required|exists:users,email',
'password' => 'required',
]);
2020-01-09 11:14:39 +01:00
2019-05-20 07:37:41 +02:00
$credentials = [
'email' => request('email'),
'password' => request('password')
];
if (Auth::attempt($credentials)) {
$success['token'] = Auth::user()->createToken('MyApp')->accessToken;
2019-05-28 17:29:15 +02:00
$success['name'] = Auth::user()->name;
2019-05-20 07:37:41 +02:00
return response()->json(['message' => $success], 200);
2019-05-20 07:37:41 +02:00
}
return response()->json(['message' => 'unauthorised'], 401);
2019-05-20 07:37:41 +02:00
}
2019-05-29 11:04:12 +02:00
/**
* log out current user
* @param Request $request
* @return json
*/
public function logout()
{
$accessToken = Auth::user()->token();
$accessToken->revoke();
return response()->json(['message' => 'signed out']);
2019-05-29 11:04:12 +02:00
}
/**
* check if a user exists
* @param Request $request [description]
* @return json
*/
public function checkUser()
{
$count = DB::table('users')->count();
return response()->json(['userCount' => $count], 200);
}
2019-05-29 11:04:12 +02:00
/**
* register new user
* @param Request $request [description]
* @return json
*/
2019-05-20 07:37:41 +02:00
public function register(Request $request)
{
// check if a user already exists
$count = DB::table('users')->count();
if( $count > 0 ) {
return response()->json(['message' => __('errors.already_one_user_registered')], 400);
}
$this->validate($request, [
2019-05-20 07:37:41 +02:00
'name' => 'required',
'email' => 'required|email',
2020-01-21 22:40:16 +01:00
'password' => 'required|confirmed|min:8',
2019-05-20 07:37:41 +02:00
]);
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
return response()->json(['message' => $success]);
2019-05-20 07:37:41 +02:00
}
2019-05-29 11:04:12 +02:00
/**
* get detailed information about a user
* @return [type] [description]
*/
2019-05-20 07:37:41 +02:00
public function getDetails()
{
2020-01-23 23:02:54 +01:00
return response()->json(Auth::user()->only('name', 'email'), 200);
2019-05-20 07:37:41 +02:00
}
2020-01-21 22:40:16 +01:00
/**
* Update the user's profile information.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$user = $request->user();
$this->validate($request, [
'name' => 'required',
2020-01-23 23:02:54 +01:00
'email' => 'required|email|unique:users,email,'.Auth::id(),
'password' => 'required',
2020-01-21 22:40:16 +01:00
]);
2020-01-23 23:02:54 +01:00
if (!Hash::check( $request->password, Auth::user()->password) ) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
tap($user)->update($request->only('name', 'email'));
return response()->json(['message' => __('auth.forms.profile_saved')]);
2020-01-21 22:40:16 +01:00
}
/**
* Update the user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2020-01-23 23:02:54 +01:00
public function updatePassword(Request $request)
2020-01-21 22:40:16 +01:00
{
$this->validate($request, [
2020-01-23 23:02:54 +01:00
'currentPassword' => 'required',
2020-01-21 22:40:16 +01:00
'password' => 'required|confirmed|min:8',
]);
2020-01-23 23:02:54 +01:00
if (!Hash::check( $request->currentPassword, Auth::user()->password) ) {
return response()->json(['message' => __('errors.wrong_current_password')], 400);
}
2020-01-21 22:40:16 +01:00
$request->user()->update([
'password' => bcrypt($request->password),
]);
2020-01-23 23:02:54 +01:00
return response()->json(['message' => __('auth.forms.password_successfully_changed')]);
2020-01-21 22:40:16 +01:00
}
2019-05-20 07:37:41 +02:00
}