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 allows the application to get the user data.

Find the repository on Github!

Let's start where we left off. We are now successfully fetching the Access Token. Let's create a Type class for the Access Token.

create AccessToken Class

Let's check the documentation to see the Access Token response.

Access Token Response

Let's create the Access Token class, looks like these are the returned types:

  1. access_token: string
  2. token_type: string
  3. expires_in: integer
  4. scope: string

access token object

Now we can initialize the Access Token Object!

Let's adjust the getAccessToken method and implement this new type.

completed getAccessToken method

There is the completed getAccessToken method. Let's put this Access Token to use!

Let's check the documentation on how to fetch the user information.

userinfo api endpoint documentation

Let's use the Authorization Header and the 'userinfo' endpoint to retrieve the user's Account ID and BattleTag.

We will do this in a new method called 'getCurrentAccount'.

getCurrentAccount method

We also added the baseApi property to the top of the page, this contains the string 'https://oauth.battle.net/'.

Let's call this method in the LarawowController.

Call the getCurrentAccount in the LarawowController

This method is now being called in the controller. Let's see if it works, head to the /login url.

undefined scope property

And we got our first error. Things were going so smooth. Let's check the documentation to see if there's any missing scopes.

battle.net

There's our scope. Looks like we need the openid scope to access the 'userinfo' endpoint. Let's implement that scope, I'll be placing it in the .env and adding it to the config.

env config

I added the 'openid' scope to my .env under 'LARAWOW_SCOPES', these scopes are 'space' separated if you have multiple.

Now let's implement this in the original redirect method.

added scopes to redirect method

Let's login again and see what response we get this time. It should take you to battle.net again to give the app your new permissions.

returned user info

Beautiful! We are now retrieving the user's battle.net info! Let's create the User type, it looks like 3 properties are returned from this endpoint:

  1. sub: string
  2. id: integer
  3. battletag: string

Let's implement this type class.

User Class

Here is the complete User class that creates a User object. Let's implement this into the getCurrentAccount method.

Completed getUserAccount method

Here is the completed 'getUserAccount' method. We are moving right along!

Hope you enjoyed part 2 of this package! All of the code is right here on Github!

Contributions are more than welcome! Next time we will login the user with their information!

Quick recap:

  1. Created the 'AccessToken' Type.
  2. Added the 'openid' scope.
  3. Created the 'getCurrentAccount' method.
  4. Created the 'User' Type.

Thanks for taking the time to read the article! Hope it helps!

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

Damion Voshall的更多文章

  • LaraWoW Part 3 - Logging In The User

    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.

  • 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…

社区洞察

其他会员也浏览了