LaraWoW Part 3 - Logging In The User
Laravel home page with battletag displayed

LaraWoW Part 3 - Logging In The User

Welcome back to part 3 of the WoW wrapper for Laravel. We're going to save the user's data to the db, and log them in.

Let's start by creating the migrations. Here is the User migration:

User Migrations

Next let's create a migration for the Access Tokens:

Access Token migration

Perfect, now let's adjust the get method in the LarawowController.

assign Access Token to the user

Here we are assigning the access token to the user, this is a method that I added to the user type that sets the access_token property to the specified parameter in the method. This creates type safety to ensure the correct AccessToken type is being assigned to the user.

save user to db in controller

We added some extra error handling for some edge cases, and we're calling on a method to save the user to the database. Let's create that method in the service.

empty updateOrCreate method

As you can see, we have a slight issue. Our User model and User type have the same name. This is why type safety is crucial. You can either import inline, or rename the import at the top of the file, I will be renaming the User type to UserType at the top of the file.

updateOrCreate method

Here is the complete method for updateOrCreate. This method will check the user's access token and create the user if there isn't one in the database, otherwise, it will update the current information stored.

Don't forget to update the User model and make our new DB columns fillable.

Let's commit the transaction and see if it's successful

commit DB transaction

Looks like we are now successfully saving the user to the database. Next we need to save the Access Token to the database.

Let's implement this into the controller as well:

add access token to db

After creating a relationship between the user and the access token, we can updateOrCreate a token like this. The eloquent model will automatically fill in the foreign key constraint and will update the first token it finds.

Finally we can log in the user!

Login the user

This will authenticate the user if they're not authenticated already, and then redirect them to the home route. Let's give it a try!

laravel logged in home page

As you can see, on the Laravel welcome page, the login link changed to the dashboard link. Looks like we successfully logged in! Let's try displaying some information.

display the user's battletag
User's battle tag showing on the Laravel Welcome page

Nice! Our battletag is correctly displaying on the welcome page! Our OAuth functionality is nearly complete!

Thanks for checking out this article, hope it helps someone out there!

要查看或添加评论,请登录

Damion Voshall的更多文章

  • LaraWoW Part 2 - Fetching The User

    LaraWoW Part 2 - Fetching The User

    Welcome to Part 2 of the LaraWoW development process! This time around, we're going to implement the functionality that…

  • LaraWoW Part 1 - Fetch Access Token

    LaraWoW Part 1 - Fetch Access Token

    Today, I am starting development on a Laravel wrapper for the battle.net API.

    3 条评论
  • Discord OAuth2 Flow in Laravel

    Discord OAuth2 Flow in Laravel

    #Discord #Laravel #Programming #WebDevelopment #PHP So, you may be wondering how sites let you log in with your Discord…

社区洞察

其他会员也浏览了