@magikauth/core
Version:
MagikAuth SDK – Auth in 2 min. Plug & Play auth for any JS framework.
145 lines (136 loc) • 3.56 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
// src/flows/emailOtp.ts
var emailOtp_exports = {};
__export(emailOtp_exports, {
sendOtp: () => sendOtp,
setPassword: () => setPassword,
verifyOtp: () => verifyOtp
});
// src/config.ts
var MAGIK_CONFIG = {
projectId: "your-project-id",
apiKey: "your-api-key",
sdkTag: "universal@0.1.0",
baseUrl: "https://magikauth.in/api"
};
// src/client.ts
var createClient = () => {
const headers = {
"X-Project-ID": MAGIK_CONFIG.projectId,
"X-API-Key": MAGIK_CONFIG.apiKey,
"X-SDK-Tag": MAGIK_CONFIG.sdkTag || "universal@0.1.0",
"Content-Type": "application/json"
};
const post = async (path, body) => {
const res = await fetch(`${MAGIK_CONFIG.baseUrl}${path}`, {
method: "POST",
headers,
body: JSON.stringify(body)
});
return res.json();
};
return { post };
};
// src/utils/session.ts
var Session = {
set(token) {
if (typeof window !== "undefined") {
localStorage.setItem("magik_token", token);
}
},
get() {
if (typeof window !== "undefined") {
return localStorage.getItem("magik_token");
}
return null;
},
clear() {
if (typeof window !== "undefined") {
localStorage.removeItem("magik_token");
}
}
};
// src/flows/emailOtp.ts
var client = createClient();
async function sendOtp(email) {
return client.post("/v1/auth", {
type: "email_otp_send",
email
});
}
async function verifyOtp(email, otp, options) {
const res = await client.post("/v1/auth", {
type: "email_otp_verify",
email,
otp
});
if (res.token) {
Session.set(res.token);
if (options?.allowPasswordIfNewUser && res.user?.is_new_user) {
res.message = "User can now set password.";
}
}
return res;
}
async function setPassword(password) {
const token = Session.get();
if (!token) return { success: false, message: "No active session." };
const res = await client.post("/v1/auth", {
type: "set_password",
password,
token
});
Session.clear();
return res;
}
// src/flows/password.ts
var password_exports = {};
__export(password_exports, {
loginWithPassword: () => loginWithPassword
});
var client2 = createClient();
async function loginWithPassword(email, password) {
const res = await client2.post("/v1/auth", {
type: "email_password_login",
email,
password
});
if (res.token) {
Session.set(res.token);
}
return res;
}
// src/magik.ts
function MagikAuth() {
return {
...emailOtp_exports,
...password_exports,
session: Session
};
}
// src/index.ts
var index_default = MagikAuth;
//# sourceMappingURL=index.js.map