technology logo
technology logo

New List

share link

by motiur dot icon Updated: Jan 29, 2023

Guide Kit Guide Kit  

<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\BaseController as BaseController;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Validator;

use App\Models\User;

use Illuminate\Support\Facades\Auth;


class RegisterController extends BaseController

{

    public function register(Request $request)

    {

        $validator = Validator::make($request->all(), [

            'name'              => 'required|string|max:255',

            'email'             => 'required|email|max:255|unique:users',

            'password'          => 'required|min:6',

            'confirm_password'  => 'required|same:password',

        ]);


        if($validator->fails()){

            return $this->sendError('Validation Error', $validator->errors());

        }

        $password = bcrypt($request->password);

       

        $user = User::create([

            'name'      => $request->name,

            'email'     => $request->email,

            'password'  => $request->password,

        ]);


        $success['token'] =$user->createToken('RestApi')->plainTextToken;

        $success['name'] =$user->name;


        return $this->sendResponse($success,'User resistrated successfully');

    }

    public function login(Request $request)

    {

        $validator = Validator::make($request->all(), [

            'email'        => 'required|email|max:255',

            'password'     => 'required|min:6',

        ]);


        if($validator->fails()){

            return $this->sendError('Validation Error', $validator->errors());

        }


        if(Auth::attempt(['email' => $request->email, 'password' =>  $request->password])){

            $user =Auth::user();

            $success['token']=$user->createToken('RestApi')->plainTextToken;

            $success['name'] =$user->name;


            return $this->sendResponse($success,'User Logged in successfully');

        }

        else{

            return $this->sendError('Unauthorized',['error'=>'Unauthorized']);

        }

    }

}