Coder Social home page Coder Social logo

casauth-flutter-sdk's Introduction

CASAuth

Build Status Pub Package Coverage Status Package Documentation Github Stars GitHub License

CASAuth Logo

CASAuth V2. A third Flutter client SDK for casdoor. Support follow platform:

platform tested example
iOS -
macOS Download
linux [-] -
Windows [-] -

Getting Started

You need install self's casdoor first. And I only test this SDK with a little version.

Version Casdoor Min Casdoor Max
v1.1.0 ✅ v1.97.0 -
v1.1.0 ✅ v1.123.0 -
v1.2.0 ✅ v1.223.0 -
v2.0.0 ✅ v1.308.0 ✅ v1.344.0
v2.1.0 ✅ v1.308.0 ✅ v1.344.0
v2.2.0 ✅ v1.308.0 ✅ v1.344.0

Quick Start

I will show howto use this SDK follow.

If casdoor server response status is error or code not 200, the SDK will thorws CASAuthError, that has 3 error level warn /error / fatal. warn error dont need process; error will case current method/request failed only; when there has fatal error, your all methods calling maybe failed.

Before start calling methods, you MUST initiate the SDK.

initiate

// init global instance
import 'package:casauth/casauth.dart';

String appId = "some-app-id";
String appName = "app-example";
String orgnazationName = "casbin";
String server = "https://door.casdoor.com";

try {
  //  Normally we use the default global instance to access casdoor.
  await init(appName, appId, server, orgnazationName);
  // Otherwise, you can create instance and manage it by your self.
  // var sdk = CASAuth(appName, appId, server, orgnazationName)
  // await sdk.init();
} catch (e) {
  debugPrint("init casauth SDK failed: $e");
}

CASAuth and global init method has optional parameters:

  • vault: Vault of Stash package, CASAuth use it to cache token, user info etc. And we will default init sqlite database if not set vault parameter. You can initiate any stash Storage Implementations and create vault to customs storage for the SDK.
  • userPrefix: Random user name prefix. Default value is mobile_.
  • redirectUri: Address configured at Casdoor application config, the value must in the application's callback list. We use it to revoke/expired JWT token. Default value is casauth.
  • logLevel: Setting SDK logger print level. If app running at flutter's kDebugMode or kProfileMode, will print stack trace info to helps debug.

Send code

When the user register or recover password, must send verification code to the target email/mobile phone.

try {
  AuthResult resp = await casauth.sendCode(email, type: AccountType.email);
} catch (e) {
  debugPrint("send code failed");
  print("error level: ${err.level}, message: ${err.message}");
}

Signup/Register by email

try {
  AuthResult resp = await casauth.registerByEmail(email, code, username: id, password: id);
  print("expect true: ${resp.code == 200}, and true: ${resp.status == "ok"}");
} on CASAuthError catch (err) {
  debugPrint("register user failed");
  print("error level: ${err.level}, message: ${err.message}");
}

Login by account

You can login with account, includes username/email/phone.

var email = "[email protected]";
var username = "me_example_com";
var password = "your_strong_password";
try {
  // email login
  AuthResult resp = await casauth.loginByAccount(email, password);
  // username login
  // resp = await casauth.loginByAccount(username, password);
  print("expect true: ${resp.code == 200}, and true: ${resp.status == "ok"}");
} on CASAuthError catch (err) {
  print("error level: ${err.level}, message: ${err.message}");
}

Logout

We call /api/logout to revoke the JWT token. You'll always see the token in Casdoor, which expires in 0s.

try {
  AuthResult resp = await casauth.logout();
  print("expect true: ${resp.code == 200}, and true: ${resp.status == "ok"}");
} on CASAuthError catch (err) {
  print("error level: ${err.level}, message: ${err.message}");
}

delete self

Soft delete account by user self, this require organization allow user edit Is deleted config. The method not clean local token, you can cancelled it before logout with the authed token.

try {
  AuthResult resp = casauth.softDeleteAccount();
  print("expect true: ${resp.code == 200}, and true: ${resp.status == "ok"}");
} on CASAuthError catch (err) {
  print("error level: ${err.level}, message: ${err.message}");
}

cancel delete self

Cancel soft delete account by user self, this require organization allow user edit Is deleted config.

try {
  AuthResult resp = casauth.cancelDeleteAccount();
  print("expect true: ${resp.code == 200}, and true: ${resp.status == "ok"}");
} on CASAuthError catch (err) {
  print("error level: ${err.level}, message: ${err.message}");
}

Recovery password

To recovery user's password, we must call API with 4 steps:

  1. get email and phone info from server via username/email/phone.
  2. send verification code with account and account type.
  3. verify code, get bool verified result and cookie.
  4. reset password with username, code, new password and cookie.

🔥🔥🔥 IMPORTANT: name of setPassword MUST is username of casdoor, NOT email or phone.

try {
  // 1. get emailAndPhone
  var info = await casauth.getEmailAndPhone(email);

  // 2. send code
  await casauth.sendCode(email,
      type: AccountType.email, method: "forget");

  // 4. reset password
  await casauth.setPassword(info.name, code, password, cookie);

  // 3. verify code
  var (verified, cookie) = await casauth.verifyCode(email, code);

  // 4. reset password
  await casauth.setPassword(info.name, code, password, cookie);
} on CASAuthError catch (err) {
  print("error level: ${err.level}, message: ${err.message}");
}

casauth-flutter-sdk's People

Contributors

kofj avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

casauth-flutter-sdk's Issues

Flutter web

Hello
I'm trying to run example code with my creentials.
It works nice on iOs, but not got bot workin on Flutter web.
Can you check and fix, please?
Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.