I'm currently in the process of developing an application that involves redirecting users to a specific URL, prompting them to open an app (with a url starting with zel:), and then sending a request back to my server. Here's the envisioned user journey:
- User clicks on the login button.
- User is directed to open an app and complete the sign-in process.
- The app sends a callback message to my server confirming the completion of the sign-in.
- I verify the user's sign-in status and then redirect them to a new path (e.g., /afterlogin).
I'm unsure about the steps needed to achieve this. Can anyone provide guidance?
Below is the code snippet I've been working on:
// pages/login.js
import axios from 'axios';
import { useRouter } from 'next/router';
export default function Login() {
const router = useRouter();
const handleClick = () => {
axios.get('https://api.runonflux.io/id/loginphrase').then(response => {
if (response.data.status === 'success') {
var loginPhrase = response.data.data;
if (loginPhrase) {
router.push(`zel:?action=sign&message=${loginPhrase}&callback=${process.env.DOMAIN}api/validateSignature`);
}
}
});
}
return (
<div>
<button onClick={handleClick}>Login</button>
</div>
)
}
This section represents the login page where users will initiate the sign-in process by clicking the login button, fetching a phrase through an API request, and getting redirected to authorize via an external app.
// pages/api/validateSignature.js
export default function handler(req, res) {
if (req.method == 'GET') {
}
var url = "https://api.runonflux.io/id/verifylogin";
var axios = require('axios');
var data = JSON.stringify({
"loginPhrase": req.body.message,
"signature": req.body.signature,
"zelid": req.body.address
});
var config = {
method: 'post',
url: url,
data: data
};
axios(config)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
res.status(200).json({ name: 'John Doe' })
}
This script manages the callback from the app.
A visual representation of the user flow can be viewed in this video - https://i.stack.imgur.com/Fn4j5.jpg