48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
|
|
import axios from 'axios';
|
|
|
|
const mAuthAPIURL = "https://api.mauth.mworld.cloud";
|
|
|
|
/**
|
|
* Creates a new mAuth authentication link.
|
|
* Requires the current origin URL.
|
|
*/
|
|
export const authLink = async (currentOrigin: string): Promise<string | null> => {
|
|
try {
|
|
const r = await axios.get(`${mAuthAPIURL}/mAuthGen?origin=${encodeURIComponent(currentOrigin)}&type=native`);
|
|
console.log(r.data);
|
|
const link = r.data?.mAuth?.auth_link;
|
|
return link || null;
|
|
} catch (err) {
|
|
console.log(err);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Fetches the data of the user who authenticated.
|
|
* Requires origin URL and the user auth token.
|
|
*
|
|
* WARNING: This CONSUMES the mAuth token. The backend `/user/check-in`
|
|
* endpoint already verifies the token server-side, so calling this during
|
|
* app bootstrap would invalidate the token before the backend can use it.
|
|
* Do NOT call this from AuthContext — it exists only for one-off flows that
|
|
* need raw mAuth profile fields without a backend round-trip.
|
|
*/
|
|
export const fetchUserData = async (origin: string, token: string) => {
|
|
try {
|
|
const payload = {
|
|
origin: origin,
|
|
required_user_data: ["fullName", "email", "phone"],
|
|
};
|
|
const r = await axios.get(
|
|
`${mAuthAPIURL}/mAuthUser/${token}?payload=${JSON.stringify(payload)}`
|
|
);
|
|
console.log(r.data);
|
|
return r.data;
|
|
} catch (err) {
|
|
console.log(err);
|
|
return null;
|
|
}
|
|
};
|