Hi,
I browsed similar questions but no luck.
We are building a SPA (Single Page App) which uses React.js on frontend and Node.js on backend and we want to authenticate our users upon successful registration.
For the registration, we are creating user accounts through our backend using Auth0 management API and that works perfectly fine but we are struggling with Resource Owner Password Flow.
When making API call from Postman, we successfully get the access_token
, refresh_token
, id_token
, token_type
, and expires_in
.
Please see the screenshot of the API call using Postman.
I just wrote a function to share here. Here’s my code:
require("dotenv").config();
const express = require("express");
const axios = require("axios").default;
const cors = require("cors");
const app = express();
const port = process.env.PORT || 4000;
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post("/login", (req, res) => {
var options = {
method: "POST",
url: `https://${process.env.AUTH0_DOMAIN}/oauth/token`,
headers: { "Content-Type": "application/x-www-form-urlencoded" },
data: {
grant_type: "password",
username: req.body.username,
password: req.body.password,
audience: process.env.AUTH0_AUDIENCE,
scope: "openid profile email",
client_id: process.env.AUTH0_CLIENT_ID,
client_secret: process.env.AUTH0_CLIENT_SECRET,
},
};
axios(options)
.then(function (response) {
console.log(response.data);
return res.status(200).json({
status: "success",
response: response.data,
});
})
.catch(function (error) {
console.error(error);
return res.status(error.response.status).json({
status: "error",
response: error.response.data,
});
});
});
app.listen(port, () =>
console.log(`Listing to server: http://localhost/${port}`)
);
Our Requirement
- Ability to authenticate the users without redirecting the users to Auth0 hosted login page.
- Ability to create user accounts through our backend using Management API and then using Resource Owner Password Flow to authenticate the users upon successful account creation through our backend.
Please help me out.
Thank you.