LaraWoW Part 2 - Fetching The User
Damion Voshall
Software Engineer @ DiscordBrowser | Contributor to Larascord, a Laravel Discord API wrapper | PHP Laravel Full-Stack Software Solutions
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.
Let's check the documentation to see the Access Token response.
Let's create the Access Token class, looks like these are the returned types:
Now we can initialize the Access Token Object!
Let's adjust the getAccessToken method and implement this new type.
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.
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'.
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.
This method is now being called in the controller. Let's see if it works, head to the /login url.
领英推荐
And we got our first error. Things were going so smooth. Let's check the documentation to see if there's any missing scopes.
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.
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.
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.
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:
Let's implement this type class.
Here is the complete User class that creates a User object. Let's implement this into the getCurrentAccount 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:
Thanks for taking the time to read the article! Hope it helps!