Untitled

mail@pastecode.io avatar
unknown
php_laravel_blade
3 years ago
3.8 kB
5
Indexable
<?php

namespace App\Http\Controllers\Authentication;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use DB;
use Auth;
use Hash;
use Illuminate\Support\Str;
use App\Models\Users;
use App\Models\PasswordResets;
class RegisterController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function Index() {
        $settings = DB::table('settings')->first();
        
        return view('authentication.register', [
            'settings' => $settings,
        ]);
    }

    public function Submit(Request $request) {
        $username = Users::where('username', $request->input('username'))->first();
        $email = Users::where('email', $request->input('email'))->first();

        if($request->input('username') == NULL) {
            return redirect()->route('auth.register')->with('error', 'Oh no! You must enter a username!')->withInput(
                $request->except('password')
            );
        } elseif($request->input('email') == NULL) {
            return redirect()->route('auth.register')->with('error', 'Oh no! You must enter an email!')->withInput(
                $request->except('password')
            );
        } elseif($request->input('password') == NULL) {
            return redirect()->route('auth.register')->with('error', 'Oh no! You must enter a password!')->withInput(
                $request->except('password')
            );
        } elseif($request->input('confirmpassword') == NULL) {
            return redirect()->route('auth.register')->with('error', 'Oh no! You must enter a password!')->withInput(
                $request->except('password')
            );
        }

        if($request->input('password') != $request->input('confirmpassword')) {
            return redirect()->route('auth.register')->with('error', 'Oh no! Your password did not match. Please try again!')->withInput(
                $request->except('password')
            );
        }

        $newusername = Str::of($request->input('username'))->slug('');

        if($username == NULL) {
            if($email == NULL) {
                $password = $request->input('password');
                $password = Hash::make($password);
        
                Users::insert([
                    'email' => $request->input('email'),
                    'username' => $newusername,
                    'password' => $password,
                    'image' => 'default.png',
                ]);

                $credentials = array(
                    'email'     => $request->input('email'),
                    'password'  => $request->input('password')
                );

                if (Auth::attempt($credentials)) {
                    $request->session()->regenerate();
        
                    return redirect()->route('base.index')->with('success', 'Welcome back, ' . Auth::user()->username . '. We have successfully logged you in!');
                }
            } else {
                return redirect()->route('auth.register')->with('error', 'Oh no! It looks like someone already has the email "' . $request->input('email') . '". Please try another!')->withInput(
                    $request->except('password')
                );
            }
        } else {
            return redirect()->route('auth.register')->with('error', 'Oh no! It looks like someone already has the username "' . $request->input('username') . '". Please try another!')->withInput(
                $request->except('password')
            );
        }
    }
}