<commit_before><?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Request;
use Auth;
use App\User;

class UsersController extends Controller {

	public function __construct() {
		$this->middleware('csrf', ['only' => ['store', 'postLogin', 'update', 'destroy']]);
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	/**
	 * Show login form
	 *
	 * @return View
	 */
	public function getLogin()
	{
		if ( Auth::check() ) {
			return redirect()->route('articles.index');
		}

		return view('users.login');
	}

	/**
	 * Authemticate user
	 *
	 * @return Response
	 */
	public function postLogin()
	{
		// Autentica el usuario

		$username = Request::input('username');
		$password = Request::input('password');

		if (Auth::attempt(['username' => $username, 'password' => $password]))
        {
            return redirect()->route('articles.index');

        } else {

        	return redirect()->back()->with('error', 'Credenciales inválidas');
        }
	}

	/**
	 * Logout user
	 *
	 * @return Response
	 */
	public function logout()
	{
		Auth::logout();

		return redirect()
			->route('articles.index');
	}

	/**<commit_msg>Added redirection to the login. Highly recomend to read the comment in the postlogin() method.<commit_after><?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Request;
use Auth;
use App\User;

class UsersController extends Controller {

	public function __construct() {
		$this->middleware('csrf', ['only' => ['store', 'postLogin', 'update', 'destroy']]);
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	/**
	 * Show login form
	 *
	 * @return View
	 */
	public function getLogin()
	{
		if ( Auth::check() ) {
			return redirect()->route('articles.index');
		}

		return view('users.login');
	}

	/**
	 * Authemticate user
	 *
	 * @return Response
	 */
	public function postLogin()
	{
		// Autentica el usuario

		$username = Request::input('username');
		$password = Request::input('password');

		if (Auth::attempt(['username' => $username, 'password' => $password]))
        {
					/*
					 *	Laravel doesn't allow to use redirect()->intended to a POST/PUT/PATCH requested URL
					 *	beacuse redirect()->intended always'll send a GET request throwing an HTTPMethodNotAllowedException.
					 *	So I decided to trick it a little bit:
					 *	First I fetch the intended URL path.
					 *	Then I take the last word (being it the RESTful endpoint).
					 *	I check if the endpoint doesnt't correspond to a GET method. If it doesn't, then redirect to the index
					 *	because that means that a form was sent and it was lost!
					 *	Finally, if it does correspond to a GET method, redirect to the page they were trying to access.
					 *
					 *	Just to get this clear: This fix allowes to redirect to a GET endpoint when logging in,
					 *	avoiding the HTTPMethodNotAllowedException that could throw if the redirection was called from
					 *	an endpoint of another HTTP method.
					 */

						$intendedRoute = redirect()->intended('articles.index')->headers->allPreserveCase()["Location"][0];
						$resourceMethod = class_basename($intendedRoute);

						if($resourceMethod != 'index' && $resourceMethod != 'show' && $resourceMethod != 'create' && $resourceMethod != 'edit') {
							return redirect()->route('articles.index');
						}

						return redirect()->to($intendedRoute);

        } else {

        	return redirect()->back()->with('error', 'Credenciales inválidas');
        }
	}

	/**
	 * Logout user
	 *
	 * @return Response
	 */
	public function logout()
	{
		Auth::logout();

		return redirect()
			->route('articles.index');
	}

	/**
{'fraction_non_alphanumeric': 0.14487817657846477, 'fraction_numerical': 0.00026198585276395077, 'mean_word_length': 3.123110151187905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6878791', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1102, 'n_words': 336}