
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@affinidi/affinidi-react-auth
Advanced tools
Affinidi react auth library for creating Affinidi Login Button
AUGMENT EXPERIENCES WITH A SAFER, SIMPLER AND MORE PRIVATE WAY TO LOGIN
A paradigm shift in the registration and sign-in process, Affinidi Login is a game-changing solution for developers. With our revolutionary passwordless authentication solution your user's first sign-in doubles as their registration, and all the necessary data for onboarding can be requested during this streamlined sign-in/signup process. End users are in full control, ensuring that they consent to the information shared in a transparent and user-friendly manner. This streamlined approach empowers developers to create efficient user experiences with data integrity, enhanced security and privacy, and ensures compatibility with industry standards.
Passwordless Authentication | Decentralised Identity Management | Uses Latest Standards |
---|---|---|
Offers a secure and user-friendly alternative to traditional password-based authentication by eliminating passwords and thus removing the vulnerability to password-related attacks such as phishing and credential stuffing. | Leverages OID4VP to enable users to control their data and digital identity, selectively share their credentials and authenticate themselves across multiple platforms and devices without relying on a centralised identity provider. | Utilises OID4VP to enhance security of the authentication process by verifying user authenticity without the need for direct communication with the provider, reducing risk of tampering and ensuring data integrity. |
Simplify the integration of Affinidi Login
into your React Application with this plugin.
Before getting started, make sure you have the following prerequisites:
Install Affinidi Vault to discover, collect, store, share, and monetise your data with consent.
Install the Affinidi CLI tool to interact with Affinidi services for managing projects, login configurations, user groups etc...
Let's start by creating a folder structure for your project. Here's an example of the directory structure:
- affinidi-apps
- client-app
- server-app
client-app
using the following command:npx create-react-app client-app
client-app
directory and open the project in your preferred code editor:cd client-app
npm start
Note: Your application will be available at http://localhost:3000/.
Now, let's integrate Affinidi Login
into your React application using the following steps:
@affinidi/affinidi-react-auth
npm package:npm install @affinidi/affinidi-react-auth
proxy
field to your package.json
like this: "proxy": "http://localhost:3001",
You can find a sample package.json
here.
src/App.js
file, import the necessary components and hooks:import React from "react";
import { AffinidiLoginButton, useAffinidiProfile } from '@affinidi/affinidi-react-auth'
useAffinidiProfile
hook to retrieve profile information once the login flow is completed:const { isLoading, error, profile, handleLogout } = useAffinidiProfile()
async function logout() {
//clear session cookie
handleLogout();
window.location.href = "/";
}
{!profile && <>
<AffinidiLoginButton />
</>}
{isLoading && <p>Loading...</p>}
{profile && <>
<button style={{ marginRight: 10 }} onClick={logout}>
Logout
</button>
<h3>User Profile</h3>
<pre style={{ textAlign: "left" }}>{JSON.stringify(profile, null, 4)}</pre>
</>}
{error && <><h2>error</h2>{error}</>}
You can find a sample App.js
here.
npm start
. This time, you should see the Affinidi Login button.Now, let's create a basic Express server for your backend API:
server-app
and navigate to it:mkdir server-app
cd server-app
package.json
file:npm init -y
You can find a sample package.json here.
express
for creating the server, dotenv
for managing environment variables, and nodemon
for automatic reloading:npm install express dotenv nodemon
index.js
file and set up a basic Express server:var express = require('express');
require('dotenv').config()
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const PORT = process.env.PORT || 3001;
const initializeServer = async () => {
app.get('/', function (req, res, next) {
res.json({ success: 'Express' });
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
}
initializeServer();
package.json
to start the server with nodemon:"start": "nodemon index.js"
npm start
Your backend server will run on http://localhost:3001/.
To complete the OAuth 2.0 Code Grant flow with Affinidi, follow these steps:
npm install @affinidi/passport-affinidi
index.js
file (inside the server-app folder), import the @affinidi/passport-affinidi
package:const { affinidiProvider } = require('@affinidi/passport-affinidi')
await affinidiProvider(app, {
id: "affinidi",
issuer: process.env.PROVIDER_ISSUER,
client_id: process.env.PROVIDER_CLIENT_ID,
client_secret: process.env.PROVIDER_CLIENT_SECRET,
redirect_uris: ['http://localhost:3000/auth/callback']
});
You can find a sample index.js file here.
Create Login configuration using the link here by giving name as Affinidi Login App
and redirect-uri as http://localhost:3000/auth/callback
Sample CLI Command
affinidi login create-config --name='Affinidi Login App' --redirect-uris='http://localhost:3000/auth/callback'
Sample response
{
"ari:identity:ap-southeast-1:d085c5a5-5765-4d8f-b00e-398f0916a161:login_configuration/0143bf454065664893b030289ae17903",
"projectId": "d085c5a5-5765-4d8f-b00e-398f0916a161",
"id": "<LOGIN_CONFIG_ID>",
"name": "Affinidi Login App",
"auth": {
"clientId": "<CLIENT_ID>",
"clientSecret": "<CLIENT_SECRET>",
"issuer": "<ISSUER>",
"tokenEndpointAuthMethod": "client_secret_post"
},
"redirectUris": [
"http://localhost:3000/auth/callback"
],
"clientMetadata": {
"name": "",
"origin": "",
"logo": ""
},
"creationDate": "2023-09-19T05:10:19Z"
}
Important: Keep the CLIENT_ID and CLIENT_SECRET safe that will be used later next step. Client Secret will only be displayed once.
Create a .env
file in the server-app
folder and add your Affinidi configuration obtained from previous step:
PROVIDER_CLIENT_ID="<CLIENT_ID>"
PROVIDER_CLIENT_SECRET="<CLIENT_SECRET>"
PROVIDER_ISSUER="<ISSUER>"
Sample .env
file displayed below after respective values of the CLIENT_ID
, CLIENT_SECRET
and ISSUER
PROVIDER_CLIENT_ID="13456678c-67ac-429a-b0d3-5c64ec4c0577"
PROVIDER_CLIENT_SECRET="AbcdeF-odZtJ9tc3oLuWVW.ECE_"
PROVIDER_ISSUER="https://apse1.api.affinidi.io/vpa/v1/login/project/d085c5a5-5765-4d8f-b00e-398f0916a161"
npm start
.client-app
folder in your terminal and run npm start
.server-app
folder in your terminal and run npm start
.Affinidi Login
button in the frontend app to start the login flow.By default, the login configuration requests only Email
data. To also retrieve the User Profile
data, follow these steps:
profile-pex.json
under server-app
folder with the contents from this file and open terminalLOGIN_CONFIG_ID
with the value obtained in a previous step:affinidi login update-config --id=LOGIN_CONFIG_ID --file=./profile-pex.json
Sample Command
affinidi login update-config --id=384192b3b3ea3df8cece307fda64cf98 --file=./profile-pex.json
Ensure that you have replaced LOGIN_CONFIG_ID
with the actual ID from your configuration.
Now that your login configuration is set up to request both Email
and User Profile
data, you can test the login flow to obtain profile information. Here are the steps:
Affinidi Login
button again to initiate the login flow.FAQs
Affinidi react auth library for creating Affinidi Login Button
We found that @affinidi/affinidi-react-auth demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.