Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature flags #17

Merged
merged 6 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
node_modules
dist
.DS_Store
coverage
35 changes: 35 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
import React from "react";

export declare function useKindeAuth(): State;

export declare function KindeProvider({
children,
}: {
children: any;
}): React.Provider<State>;

export declare function handleAuth(): any;

export type User = {
id: string;
name: string | null;
email: string | null;
given_name: string | null;
family_name: string | null;
updated_at: string | null;
picture: string | null;
};

export type State = {
user: User;
isLoading: boolean;
isAuthenticated: boolean;
error?: string | undefined;
};

export type KindePermissions = {
permissions: string[];
orgCode: string;
};

export type KindePermission = {
isGranted: boolean;
orgCode: string;
};

export type KindeFlagTypeCode = "b" | "i" | "s";

export type KindeFlagTypeValue = "boolean" | "integer" | "string";

export type KindeFlag = {
code: string;
type: KindeFlagTypeValue | null;
value: any;
defaultValue: any | null;
is_default: boolean;
};

export type KindeOrganization = {
orgCode: string;
};

export type KindeOrganizations = {
orgCodes: string[];
};
100 changes: 97 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
{
"name": "@kinde-oss/kinde-auth-nextjs",
"version": "1.1.0",
"version": "1.2.0",
"description": "Kinde Auth SDK for NextJS",
"main": "bundle.js",
"types": "index.d.ts",
"main": "dist/kinde-auth-nextjs.common.js",
"module": "dist/kinde-auth-nextjs.esm.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"test": "jest",
"build": "rollup -c"
"build": "genversion --es6 src/utils/version.js && rollup -c",
"test": "jest"
},
"author": {
"name": "Kinde",
"email": "engineering@kinde.com",
"url": "https://kinde.com"
},
"repository": {
"type": "git",
"url": "https://github.com/kinde-oss"
"url": "https://github.com/kinde-oss/kinde-auth-nextjs"
},
"author": "",
"license": "MIT",
"bugs": "https://github.com/kinde-oss/kinde-auth-nextjs",
"homepage": "https://kinde.com",
"devDependencies": {
"@babel/core": "^7.17.12",
"@babel/preset-react": "^7.17.12",
"@rollup/plugin-babel": "^5.3.1",
"genversion": "^3.1.1",
"rollup": "^2.73.0",
"rollup-plugin-terser": "^7.0.2"
},
Expand Down
14 changes: 10 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { terser } from "rollup-plugin-terser";
export default {
plugins: [babel({ babelHelpers: "bundled" }), terser()],
input: "src/index.js",
output: {
file: "bundle.js",
format: "cjs",
},
output: [
{
file: "dist/kinde-auth-nextjs.common.js",
format: "cjs",
},
{
file: "dist/kinde-auth-nextjs.esm.js",
format: "es",
},
],
};
3 changes: 3 additions & 0 deletions src/handlers/callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import jwt_decode from "jwt-decode";
import { config } from "../config/index";
import { version } from "../utils/version";

var cookie = require("cookie");

export const callback = async (req, res) => {
Expand All @@ -16,6 +18,7 @@ export const callback = async (req, res) => {
method: "POST",
headers: new Headers({
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"Kinde-SDK": `"NextJS"/${version}`,
}),
body: new URLSearchParams({
client_id: config.clientID,
Expand Down
9 changes: 1 addition & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
export { KindeProvider, useKindeAuth } from "./frontend/index";
export { handleAuth } from "./handlers/index";
export {
getAccessToken,
getIdToken,
getOrganization,
getPermission,
getPermissions,
getUserOrganizations,
} from "./session/index";
export * from "./session/index";
10 changes: 10 additions & 0 deletions src/session/getBooleanFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFlag } from "./getFlag";

export const getBooleanFlag = (code, defaultValue) => {
try {
const flag = getFlag(code, defaultValue, "b");
return flag.value;
} catch (err) {
console.error(err);
}
};
37 changes: 37 additions & 0 deletions src/session/getFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getClaim } from "./getClaim";

const flagDataTypeMap = {
s: "string",
i: "integer",
b: "boolean",
};

const getClaimValue = (claim, tokenKey = "access_token") => {
const obj = getClaim(claim, tokenKey);
return obj && obj.value;
};

export const getFlag = (code, defaultValue, flagType) => {
const flags = getClaimValue("feature_flags");
const flag = flags && flags[code] ? flags[code] : {};

if (!flag.v && !defaultValue) {
throw Error(
`Flag ${code} was not found, and no default value has been provided`
);
}

if (flagType && flag.t && flagType !== flag.t) {
throw Error(
`Flag ${code} is of type ${flagDataTypeMap[flag.t]} - requested type ${
flagDataTypeMap[flagType]
}`
);
}
return {
code,
type: flagDataTypeMap[flag.t || flagType],
value: flag.v == null ? defaultValue : flag.v,
is_default: flag.v == null,
};
};
10 changes: 10 additions & 0 deletions src/session/getIntegerFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFlag } from "./getFlag";

export const getIntegerFlag = (code, defaultValue) => {
try {
const flag = getFlag(code, defaultValue, "i");
return flag.value;
} catch (err) {
console.error(err);
}
};
10 changes: 10 additions & 0 deletions src/session/getStringFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFlag } from "./getFlag";

export const getStringFlag = (code, defaultValue) => {
try {
const flag = getFlag(code, defaultValue, "s");
return flag.value;
} catch (err) {
console.error(err);
}
};
4 changes: 4 additions & 0 deletions src/session/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export { getAccessToken } from "./getAccessToken";
export { getBooleanFlag } from "./getBooleanFlag";
export { getFlag } from "./getFlag";
export { getIdToken } from "./getIdToken";
export { getIntegerFlag } from "./getIntegerFlag";
export { getOrganization } from "./getOrganization";
export { getPermission } from "./getPermission";
export { getPermissions } from "./getPermissions";
export { getStringFlag } from "./getStringFlag";
export { getUserOrganizations } from "./getUserOrganizations";
2 changes: 2 additions & 0 deletions src/utils/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.