Skip to content

klarna/socialite-provider-klarna-example

Repository files navigation

Sign in with Klarna using Laravel Socialite Provider

Introduction

This is an example Laravel application that demonstrates the usage of Sign in with Klarna Laravel Socialite Provider.

Running the application

  1. Create an .env file by copying the .env.example file.
  2. Replace KLARNA credentials in the .env file.
KLARNA_CLIENT_ID=""
KLARNA_CLIENT_SECRET=""
KLARNA_REDIRECT_URI=""

You can get the client ID and client secret from merchant portal (generate a new API key and use it as a secret).

The redirect URI should be the URL of the application where the user will be redirected after the authentication. It should be a backend route that will handle the authentication response. The response from Klarna will contain the user details and the refresh token using which you can create a user account in your database.

📕 Note: Make sure you register your redirect URI as a valid redirect URI in the merchant portal. Ask your Klarna contact to do this for you.

  1. Install the dependencies using composer.
composer install
  1. Run the application.
php artisan serve
  1. Visit the application in the browser.
http://localhost:8000/login
  1. Click on the "Sign in with Klarna" button to start the authentication process.

Understanding the code

Follow the official installation documentation of Socialite Providers.

  1. Add the Klarna provider configuration in the config/services.php file.
'klarna' => [
  'client_id' => env('KLARNA_CLIENT_ID'),
  'client_secret' => env('KLARNA_CLIENT_SECRET'),
  'redirect' => env('KLARNA_REDIRECT_URI'),
]
  1. Add Socialite Manager in the bootstrap providers
<?php

return [
  // other existing providers
  \SocialiteProviders\Manager\ServiceProvider::class
];
  1. Setup controller and redirect the user to Klarna for authentication.

Follow the code in redirectToKlarna function in KlarnaLoginController.php file.

Add the required scopes and redirect the user

public function redirectToProvider() {
  return Socialite::driver('klarna')->scopes([
    'openid', 'profile:name', 'profile:email', 'profile:phone'
  ])->redirect();
}

📕 Note: Do not forget to always add openid scope.

  1. Handle the callback

Follow the code in handleCallback function in KlarnaLoginController.php file.

Fetch user details received from Klarna and create a user account in your database.

$user = Socialite::driver('klarna')->user();

The fields on the user objects are as documented in Socialiate Klarna Provider documentation.

Get the refresh token from the user object.

$refreshToken = $user->refreshToken;

Button styling

If you start the application, you will see the different buttons that you can use and copy the CSS from the code section.

Always follow the latest Button Styling Guide documentation.

About

An example of how to integrate klarna as a login provider in socialite

Resources

License

Stars

Watchers

Forks

Languages