Skip to content

Commit

Permalink
fix: fix first login not logging in
Browse files Browse the repository at this point in the history
This fixes the issue where the first login (register) somehow didn't happen. When you press 'Login'
a second time, user was logged in.

fix #408
  • Loading branch information
daveroverts committed Feb 13, 2022
1 parent 85711c8 commit d0eda66
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 22 additions & 17 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function login(Request $request)
return redirect()->away($authorizationUrl);
} elseif ($request->input('state') !== session()->pull('oauthstate')) { // State mismatch, error
flashMessage('error', 'Login failed', 'Something went wrong, please try again');
return redirect('/')->withError("Something went wrong, please try again.");
return redirect(route('home'));
} else { // Callback (user has just logged in Connect)
return $this->verifyLogin($request);
}
Expand All @@ -72,7 +72,7 @@ protected function verifyLogin(Request $request)
]);
} catch (IdentityProviderException $e) {
flashMessage('error', 'Login failed', 'Something went wrong, please try again');
return redirect('/')->withError("Something went wrong, please try again later.");
return redirect(route('home'));
}
$resourceOwner = json_decode(json_encode($this->provider->getResourceOwner($accessToken)->toArray()));

Expand All @@ -91,36 +91,40 @@ protected function verifyLogin(Request $request)
!$data['email']
) {
flashMessage('error', 'Login failed', 'We need you to grant us all marked permissions');
return redirect('/')->withError("We need you to grant us all marked permissions");
return redirect(route('home'));
}

$this->completeLogin($data, $accessToken);

if (session('booking')) {
$booking = Booking::whereUuid(session('booking'))->first();
session()->forget('booking');
if (!empty($booking)) {
if ($booking->status !== BookingStatus::BOOKED) {
return redirect()->intended(route('bookings.edit', $booking))->withSuccess('Login Successful');
return redirect(route('bookings.edit', $booking));
}
return redirect()->intended(route('bookings.show', $booking))->withSuccess('Login Successful');
return redirect(route('bookings.show', $booking));
}
} elseif (session('event')) {
$event = Event::whereSlug(session('event'))->first();
session()->forget('event');
if (!empty($event)) {
return redirect()->intended(route('events.show', $event))->withSuccess('Login Successful');
return redirect(route('events.show', $event));
}
}
return redirect()->intended('/')->withSuccess('Login Successful');
return redirect(route('home'));
}

protected function completeLogin($data, $token)
protected function completeLogin($data, $token): User
{
$account = User::firstOrNew(['id' => $data['cid']]);
$account->id = $data['cid'];
$account->name_first = $data['first_name'];
$account->name_last = $data['last_name'];
$account->email = $data['email'];
$account = User::updateOrCreate(
['id' => $data['cid']],
[
'name_first' => $data['first_name'],
'name_last' => $data['last_name'],
'email' => $data['email'],
]
);

if ($token->getToken() !== null) {
$account->access_token = $token->getToken();
Expand All @@ -133,15 +137,16 @@ protected function completeLogin($data, $token)
}

$account->save();
auth()->login($account, true);
activity()->causedBy(auth()->user())->log('Login');
auth()->loginUsingId($data['cid'], true);
activity()->log('Login');

return $account;
}

public function logout()
{
activity()->causedBy(auth()->id())->log('Logout');
activity()->log('Logout');
auth()->logout();
return redirect('/');
return redirect(route('home'));
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class User extends Authenticatable
use Notifiable;

protected $guarded = [
'id', 'isAdmin'
'isAdmin'
];
/**
* The attributes that should be hidden for arrays.
Expand Down

0 comments on commit d0eda66

Please sign in to comment.