4

I created a new project in Laravel that consumes all data from an API. For private data like a user profile, I need an access token to get the data.

Once I have an access token, how do I set the token as Auth::id() in Laravel? Or perhaps I can store the user profile as Auth::user() so that I can use @auth in a frontend blade file?

class CustomAuthController extends Controller
{
    public function index()
    {
        return view('login');
    }

    public function store(Request $request)
    {
        $request->validate([
            'phone' => 'required|numeric'
        ]);

        $data = [
            'phone' => $request->phone
        ];
        $codeSent = GeneralFunction::WebRequestPublicApi('first-login', $data, null, null, null, true);
        if($codeSent->status == "success")
        {
            return redirect('verify');
        } else {
            $errors = new MessageBag();
            $errors->add("phone", "Invalid phone number");
            return view('login')->withErrors($errors);
        }
    }

    public function showVerify()
    {
        return view('verify');
    }

    public function verify(Request $request)
    {
       try {
            $request->validate([
                'verify' => 'required|size:6'
            ]);
            $data = [
                'token_code' => $request->verify,
                'source' => 'web'
            ];
            $token = GeneralFunction::WebRequestPublicApi('verify-login', $data, null, null, null, true);
            if($token->status === "success")
            {
                $userData  = GeneralFunction::WebRequestPublicApi('membership', null, 'GET', null, null, true,  $token->results->access_token);

                if($userData->status !== "error")
                {
                    $user = (array) $userData->results[0];
                    $request->session()->put('token', $token->results->access_token);
                    Auth::attempt($user, false, false);

                    return redirect('/');
                }
            } else {
                $errors = new MessageBag();
                $errors->add("verify", "Invalid Token");
                return view('verify')->withErrors($errors);
            }
       } catch (Exception $e) {
            $errors = new MessageBag();
            $errors->add("verify", $e->getMessage());
            return view('verify')->withErrors($errors);
       }
    }
}

I tried using Auth::attempt, Auth::login(), and the other method, but all of these required a user table. My project does not have a database.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
naticap
  • 379
  • 1
  • 5
  • 19

2 Answers2

0

You can do something like following.

In the controller

if($auth_ok)
{
    session(['user' => ['key' => 'value', 'key2' => 'value2'] ]); // set session data
    return view('frontend');
}

In the view

$user = session('user', false);

@if(!$user) // if not logged in

do something

@else // logged in successfully

Welcome my user

@endif

Hope this helps.

Mahbubul Islam
  • 998
  • 1
  • 10
  • 24
  • Yes, it help mahbubul. but i still asking? is another way to use official custom Auth that laravel provide, so I can use `@auth` in frontend. But Thank you for your help Mahbubul – naticap Nov 02 '19 at 05:30
  • Well without database i think this is the simple way. Other wise you will need more lengthy and customized solution. – Mahbubul Islam Nov 02 '19 at 05:50
  • You can use custom guard or middleware approach. You can see the Laravel documentation for that. – Mahbubul Islam Nov 02 '19 at 06:04
  • 1
    Finally I use your Advice Mahbubul, because I think to much time to research.. thank you very much Mahbubul – naticap Nov 13 '19 at 02:32
  • I am trying to achieve the same exact thing - did you end up setting session data or creating a custom Auth guard? – Luke Galea Nov 06 '20 at 09:52
0

i guess the best thing you need to do is to use sqlite and once you got login from your api create a new user from it or find if there is existing already and Auth::login($newUser);

KevDev
  • 541
  • 2
  • 6
  • 20