Sentry on Laravel with JSON (code sample)

Submitted by Mahmoud_Zalt - 9 years ago

This is not how I write code on a daily basis, I brought everything to one place JUST OF THE PURPOS OF demonstrating how this stuff work. And in this example they do work:

<?php

class UserController extends BaseController {


    /*
    |--------------------------------------------------------------------------
    | Register new user
    |--------------------------------------------------------------------------
    */

    public function postRegistration()
    {

        $validation_rules = array(
            'first_name' => 'required|between:2,16',
            'last_name' => 'between:2,16',
            'email' => 'required|email',
            'password' => 'required|alpha_num|min:4|confirmed',
            'password_confirmation' => 'required|alpha_num|min:4',
        );

        // validate all inputs
        $validator = Validator::make(Input::all(), $validation_rules);

        if(!$validator->fails()){

            try
            {
                // Create and register the new user
                $user = Sentry::register(array(
                    'first_name'    => Input::get('first_name'),
                    'last_name'     => Input::get('last_name'),
                    'email'         => Input::get('email'),
                    'password'      => Input::get('password'),
                ));

                //          get the activation code
                $activation_code = $user->getActivationCode();

                //          create an activation url
                $activation_url = route('activation') . "?id=" . $user->id . "&code=" . $activation_code;

                // just for testing - disable emails sending
                $testing = false;

                if($testing != true){

                    try {
                        // send the email
                        Mail::queue('emails.confirmation', array('name' => Input::get('first_name'), 'url' => $activation_url), function($message)
                        {
                            $message->to(Input::get('email'), Input::get('first_name'))->subject('Email Confirmation');
                        });

                        Sentry::logout();

                        $response = array(
                            'status' => 'success',
                            'type' => 'string',
                            'message' => 'Registration Complete',
                            'redirect_url' => route('after_register'),
                        );

                        return Response::json($response);

                    } catch (Exception $e) {
                        // if the mail wasn't send
                        // delete the user
                        $user->delete();
                        // ask for registration again

                        $msg = 'Something went wrong! Please try again.';

                        $response = array(
                            'status' => 'failed',
                            'type' => 'string',
                            'message' => $msg,
                            'redirect_url' => route('home'),
                        );

                        return Response::json($response);
                    }

                }else{
                    $msg = 'I am on Testing. I wont send the confirmation email.';

                    $response = array(
                        'status' => 'success',
                        'type' => 'string',
                        'message' => $msg,
                        'redirect_url' => route('home'),
                    );

                    return Response::json($response);
                }

            }
            catch (Exception $e)
            {
                $msg = 'This e-mail already registered.';

                $response = array(
                    'status' => 'failed',
                    'type' => 'string',
                    'message' => $msg,
                    'redirect_url' => route('home'),
                );

                return Response::json($response);

            }

        }else{

            $msg = $validator->messages()->toJson();

            $response = array(
                'status' => 'failed',
                'type' => 'json',
                'message' => $msg,
                'redirect_url' => route('home'),
            );

            return Response::json($response);

        }



    }




    /*
    |--------------------------------------------------------------------------
    | Activate account after confirming email address
    |--------------------------------------------------------------------------
    */

    public function activation()
    {

//        $error_message = 'Congratulation Email Confirmed, you can now login';

        try
        {
            // Find the user using the user id
            $user = Sentry::findUserById(Input::get('id'));

            // Attempt to activate the user
            if ($user->attemptActivation(Input::get('code')))
            {

                // Log the user in
                Sentry::login(Sentry::findUserById(Input::get('id')));

                // User activation passed
                return Redirect::route('home');
            }
            else
            {
                // User activation failed
                $message['message_type'] = 'error';
                $message['message_text'] = 'Activation Failed';

                return Redirect::route('home')
                    ->with('message', $message);

            }
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
        {
            $error_message = 'User was not found.';
        }
        catch (Cartalyst\Sentry\Users\UserAlreadyActivatedException $e)
        {
            $error_message = 'User is already activated.';
        }

        $message['message_type'] = 'error';
        $message['message_text'] = $error_message;

        return Redirect::route('home')
            ->with('message', $message);

    }




    /*
    |--------------------------------------------------------------------------
    | Login the user
    |--------------------------------------------------------------------------
    */

    public function postLogin()
    {

        $rules = array(
            'email' => 'required|email',
            'password' => 'required'
        );

        $validator = Validator::make(Input::all(), $rules);

        if(!$validator->fails()){

            try
            {
                // Login credentials
                $credentials = array(
                    'email'     => Input::get('email'),
                    'password'  => Input::get('password'),
                );

                // Authenticate the user
                $user = Sentry::authenticate($credentials, true);

                if($user){

                    $response = array(
                        'status' => 'success',
                        'type' => 'string',
                        'message' => 'Login Complete',
                        'redirect_url' => route('home'),
                    );

                    return Response::json($response);

                }

            }

            catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
            {
                $msg = 'Email or Password is incorrect.';

                $response = array(
                    'status' => 'failed',
                    'type' => 'string',
                    'message' => $msg,
                    'redirect_url' => route('home'),
                );

                return Response::json($response);
            }
            catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
            {
                $msg = 'Email or Password is incorrect.';

                $response = array(
                    'status' => 'failed',
                    'type' => 'string',
                    'message' => $msg,
                    'redirect_url' => route('home'),
                );

                return Response::json($response);
            }
            catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
            {

                $msg = 'User is not activated. Go to your inbox and confirm your email.';

                $response = array(
                    'status' => 'failed',
                    'type' => 'string',
                    'message' => $msg,
                    'redirect_url' => route('home'),
                );

                return Response::json($response);
            }

        }else{

            $msg = $validator->messages()->toJson();

            $response = array(
                'status' => 'failed',
                'type' => 'json',
                'message' => $msg,
                'redirect_url' => route('home'),
            );

            return Response::json($response);
        }

    }


    /*
    |--------------------------------------------------------------------------
    | Logout the user
    |--------------------------------------------------------------------------
    */
    public function logout()
    {
        Sentry::logout();

        return Redirect::route('home');
    }



}