2016-10-10 10:57:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
2016-10-10 12:26:55 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-10-10 10:57:39 +02:00
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
|
|
|
|
class UserController {
|
|
|
|
|
|
|
|
private $auth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance for 'auth'.
|
|
|
|
*
|
|
|
|
* @param Guard $auth
|
|
|
|
*/
|
|
|
|
public function __construct(Guard $auth)
|
|
|
|
{
|
|
|
|
$this->auth = $auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Login user and return correct response.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
$username = Input::get('username');
|
|
|
|
$password = Input::get('password');
|
|
|
|
|
|
|
|
if($this->auth->attempt(['username' => $username, 'password' => $password], true)) {
|
|
|
|
return response('Success', 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response('Unauthorized', 401);
|
|
|
|
}
|
|
|
|
|
2016-10-10 12:26:55 +02:00
|
|
|
/**
|
2016-10-10 15:14:57 +02:00
|
|
|
* Save new user credentials.
|
2016-10-10 12:26:55 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-10-14 12:50:05 +02:00
|
|
|
public function changeUserData()
|
|
|
|
{
|
2016-10-10 15:14:57 +02:00
|
|
|
$username = Input::get('username');
|
|
|
|
$password = Input::get('password');
|
2016-10-10 12:26:55 +02:00
|
|
|
|
|
|
|
$user = Auth::user();
|
2016-10-10 15:14:57 +02:00
|
|
|
$user->username = $username;
|
|
|
|
|
|
|
|
if($password != '') {
|
|
|
|
$user->password = bcrypt($password);
|
|
|
|
}
|
2016-10-10 12:26:55 +02:00
|
|
|
|
|
|
|
if($user->save()) {
|
|
|
|
return response('Success', 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response('Server Error', 500);
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:57:39 +02:00
|
|
|
/**
|
|
|
|
* Logout user and redirect to home.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
$this->auth->logout();
|
|
|
|
|
|
|
|
return redirect('/');
|
|
|
|
}
|
2017-01-24 16:11:47 +01:00
|
|
|
}
|