-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: general implement ref to: https://github.com/casbin/casbin-oa/blob/master/web/src/auth/Auth.js Signed-off-by: Zxilly <[email protected]> feat: export correspond interface Signed-off-by: Zxilly <[email protected]> * feat: add option for redirect url Signed-off-by: Zxilly <[email protected]> * refactor: a higher degree of customization Signed-off-by: Zxilly <[email protected]> * feat: adjust for new API Signed-off-by: Zxilly <[email protected]>
- Loading branch information
Showing
8 changed files
with
600 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
.idea/ | ||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "casdoor-js-sdk", | ||
"version": "1.0.0", | ||
"main": "lib/cjs/index.js", | ||
"typings": "lib/cjs/index.d.ts", | ||
"module": "lib/esm/index.js", | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"build": "run-s clean && run-p build:*", | ||
"build:cjs": "tsc -p tsconfig.cjs.json", | ||
"build:esm": "tsc -p tsconfig.esm.json", | ||
"clean": "rimraf lib" | ||
}, | ||
"devDependencies": { | ||
"npm-run-all": "^4.1.5", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import sdk from './sdk'; | ||
|
||
export default sdk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export interface sdkConfig { | ||
serverUrl: string, // your Casdoor URL, like the official one: https://door.casbin.com | ||
clientId: string, // your Casdoor OAuth Client ID | ||
appName: string, // your Casdoor application name, like: "app-built-in" | ||
organizationName: string // your Casdoor organization name, like: "built-in" | ||
} | ||
|
||
// reference: https://github.com/casdoor/casdoor-go-sdk/blob/90fcd5646ec63d733472c5e7ce526f3447f99f1f/auth/jwt.go#L19-L32 | ||
export interface accountSession { | ||
organization: string, | ||
username: string, | ||
type: string, | ||
name: string, | ||
avatar: string, | ||
email: string, | ||
phone: string, | ||
affiliation: string, | ||
tag: string, | ||
language: string, | ||
score: number, | ||
isAdmin: boolean, | ||
accessToken: string | ||
} | ||
|
||
class Sdk { | ||
private config: sdkConfig | ||
|
||
constructor(config: sdkConfig) { | ||
this.config = config | ||
} | ||
|
||
public getSignupUrl() { | ||
return `${this.config.serverUrl.trim()}/signup/${this.config.appName}`; | ||
} | ||
|
||
public getSigninUrl(redirectUri: string = `${window.location.origin}/callback`): string { | ||
const scope = "read"; | ||
const state = this.config.appName; | ||
return `${this.config.serverUrl.trim()}/login/oauth/authorize?client_id=${this.config.clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}&state=${state}`; | ||
} | ||
|
||
public getUserProfileUrl(userName: string, account: accountSession) { | ||
let param = ""; | ||
if (account !== undefined && account !== null) { | ||
param = `?access_token=${account.accessToken}`; | ||
} | ||
return `${this.config.serverUrl.trim()}/users/${this.config.organizationName}/${userName}${param}`; | ||
} | ||
|
||
public getMyProfileUrl(account: accountSession) { | ||
let param = ""; | ||
if (account !== undefined && account !== null) { | ||
param = `?access_token=${account.accessToken}`; | ||
} | ||
return `${this.config.serverUrl.trim()}/account${param}`; | ||
} | ||
} | ||
|
||
export default Sdk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig", | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"module": "CommonJS", | ||
"outDir": "lib/cjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig", | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"module": "ESNext", | ||
"outDir": "lib/esm" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES6", | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"strict": true, | ||
"strictPropertyInitialization": false, | ||
"declaration": true, | ||
"downlevelIteration": true, | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
Oops, something went wrong.