Skip to content

Commit

Permalink
feat: add initial code (#1)
Browse files Browse the repository at this point in the history
* feat: add init code for sdk.

* Delete .idea/.gitignore

* Delete .idea/casdoor-react-native-sdk.iml

* Delete .idea/modules.xml

* Delete .idea/vcs.xml

* Update package.json

* Update package.json

---------

Co-authored-by: hsluoyz <[email protected]>
  • Loading branch information
cwp0 and hsluoyz authored Sep 2, 2023
1 parent 4a14eeb commit c7dd70f
Show file tree
Hide file tree
Showing 13 changed files with 6,651 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/semantic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Always validate the PR title AND all the commits
titleAndCommits: true
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build

on: [push, pull_request]

jobs:
build:
name: SDK build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14.17.0'

- run: yarn install

- run: yarn test

- run: yarn coverage

- run: yarn build

- name: Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
17 changes: 17 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Release
on:
push:
branches:
- master
jobs:
semantic-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Run semantic-release
if: github.event_name == 'push' && github.repository == 'casdoor/casdoor-react-native-sdk'
run: yarn install && yarn semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
24 changes: 24 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"debug": true,
"branches": [
"+([0-9])?(.{+([0-9]),x}).x",
"master",
{
"name": "rc"
},
{
"name": "beta",
"prerelease": true
},
{
"name": "alpha",
"prerelease": true
}
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/npm",
"@semantic-release/github"
]
}
160 changes: 159 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,159 @@
# casdoor-react-native-sdk
# casdoor-react-native-sdk

[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[![codebeat badge](https://codebeat.co/badges/6f2ad052-7fc8-42e1-b40f-0ca2648530c2)](https://codebeat.co/projects/github-com-casdoor-casdoor-react-native-sdk-master)
[![GitHub Actions](https://github.com/casdoor/casdoor-react-native-sdk/actions/workflows/release.yml/badge.svg)](https://github.com/casdoor/casdoor-react-native-sdk/actions/workflows/release.yml)
[![GitHub Actions](https://github.com/casdoor/casdoor-react-native-sdk/actions/workflows/build.yml/badge.svg)](https://github.com/casdoor/casdoor-react-native-sdk/actions/workflows/build.yml)
[![Coverage Status](https://codecov.io/gh/casdoor/casdoor-react-native-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/casdoor/casdoor-react-native-sdk)
[![Release](https://img.shields.io/github/release/casdoor/casdoor-react-native-sdk.svg)](https://github.com/casdoor/casdoor-react-native-sdk/releases/latest)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/5rPsrAzK7S)

[npm-image]: https://img.shields.io/npm/v/casdoor-react-native-sdk.svg?style=flat-square

[npm-url]: https://npmjs.com/package/casdoor-react-native-sdk

[download-image]: https://img.shields.io/npm/dm/casdoor-react-native-sdk.svg?style=flat-square

[download-url]: https://npmjs.com/package/casdoor-react-native-sdk
This is Casdoor's SDK for react-native will allow you to easily connect your application to the Casdoor authentication system
without having to implement it from scratch.

Casdoor SDK is very simple to use. We will show you the steps below.

## Usage in NPM environment

### Installation

~~~shell script
# NPM
npm i casdoor-react-native-sdk

# Yarn
yarn add casdoor-react-native-sdk
~~~

### Init SDK

Initialization requires 5 parameters, which are all string type:

| Name (in order) | Must | Description |
| ---------------- | ---- | --------------------------------------------------- |
| serverUrl | Yes | your Casdoor server URL |
| clientId | Yes | the Client ID of your Casdoor application|
|clientSecret|Yes|the Client Secret of your Casdoor application|
| appName | Yes | the name of your Casdoor application |
| organizationName | Yes | the name of the Casdoor organization connected with your Casdoor application |
| redirectPath | No | the path of the redirect URL for your Casdoor application, will be `/callback` if not provided |
| signinPath | No | the path of the signin URL for your Casdoor application, will be `/api/signin` if not provided |

```typescript
import SDK from 'casdoor-react-native-sdk'

const sdkConfig = {
serverUrl: 'https://door.casdoor.com',
clientId: 'b800a86702dd4d29ec4d',
clientSecret: '1219843a8db4695155699be3a67f10796f2ec1d5',
appName: 'app-example',
organizationName: 'casbin',
redirectPath: 'http://localhost:5000/callback',
signinPath: '/api/signin',
};
const sdk = new SDK(sdkConfig)
// call sdk to handle
```

## Usage in vanilla Javascript

### Import and init SDK

Initialization parameters are consistent with the previous node.js section:

```javascript
<!--init the SDK-->
import SDK from 'casdoor-react-native-sdk'
const sdkConfig = {
serverUrl: 'https://door.casdoor.com',
clientId: 'b800a86702dd4d29ec4d',
clientSecret: '1219843a8db4695155699be3a67f10796f2ec1d5',
appName: 'app-example',
organizationName: 'casbin',
redirectPath: 'http://localhost:5000/callback',
signinPath: '/api/signin',
};
const sdk = new SDK(sdkConfig)
```

### Call functions in SDK

```javascript
// call sdk to handle
sdk.getSignupUrl();
```

## API reference interface

#### Get sign up url

```typescript
getSignupUrl()
```

Return the casdoor url that navigates to the registration screen

#### Get sign in url

```typescript
getSigninUrl()
```

Return the casdoor url that navigates to the login screen

#### Get user profile page url

```typescript
getUserProfileUrl(userName, account)
```

Return the url to navigate to a specific user's casdoor personal page

#### Get my profile page url

```typescript
getMyProfileUrl(account)
```

#### getAccessToken

```typescript
getAccessTokken(redirectUrl); // http://localhost:5000/callback?code=b75bc5c5ac65ffa516e5&state=gjmfdgqf498
```

Handle the callback url from casdoor, call the back-end api to complete the login process

#### Determine whether silent sign-in is being used

```typescript
isSilentSigninRequested()
```

We usually use this method to determine if silent login is being used. By default, if the silentSignin parameter is included in the URL and equals one, this method will return true. Of course, you can also use any method you prefer.

#### silentSignin

````typescript
silentSignin(onSuccess, onFailure)
````

First, let's explain the two parameters of this method, which are the callback methods for successful and failed login. Next, I will describe the execution process of this method. We will create a hidden "iframe" element to redirect to the login page for authentication, thereby achieving the effect of silent sign-in.

#### JwtDecode

````typescript
JwtDecode(jwtToken)
````


## More examples

To see how to use SDK, you can refer to [casdoor-react-native-example](https://github.com/casdoor/casdoor-react-native-example).
82 changes: 82 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"name": "casdoor-react-native-sdk",
"version": "1.0.0",
"description": "React-Native client SDK for Casdoor",
"main": "lib/cjs/index.js",
"typings": "lib/cjs/index.d.ts",
"module": "lib/esm/index.js",
"license": "Apache-2.0",
"scripts": {
"prepack": "run-s build",
"postpack": "run-s clean",
"build": "run-s clean && run-p build:*",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.esm.json",
"clean": "rimraf lib",
"test": "jest",
"coverage": "jest --coverage",
"semantic-release": "semantic-release"
},
"jest": {
"maxConcurrency": 1,
"maxWorkers": 1,
"testTimeout": 30000,
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
},
"devDependencies": {
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/commit-analyzer": "^8.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.2.3",
"@semantic-release/npm": "^7.1.3",
"@semantic-release/release-notes-generator": "^9.0.3",
"@types/jest": "^27.0.2",
"jest": "^27.2.1",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"semantic-release": "^17.4.4",
"ts-jest": "^27.0.5",
"typescript": "^4.5.5"
},
"files": [
"lib"
],
"keywords": [
"auth",
"authn",
"authentication",
"sso",
"oauth",
"oidc",
"casbin",
"casdoor"
],
"repository": {
"type": "git",
"url": "https://github.com/casdoor/casdoor-react-native-sdk.git"
},
"author": {
"name": "Wenpeng Chen",
"email": "[email protected]"
},
"bugs": {
"url": "https://github.com/casdoor/casdoor-react-native-sdk/issues"
},
"homepage": "https://github.com/casdoor/casdoor-react-native-sdk",
"dependencies": {
"@react-native-async-storage/async-storage": "^1.19.3",
"jwt-decode": "^3.1.2"
}
}
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Sdk from './sdk';

export default Sdk;
Loading

0 comments on commit c7dd70f

Please sign in to comment.