Giter Club home page Giter Club logo

authing-go-sdk's Introduction

authing-go-sdk

What is Authing

Authing is an IDaaS which is created by Ivy.

Installation

Make sure you have a working Go environment. To install authing-go-sdk, simply run:

go get github.com/Authing/graphql

Quick Guide

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"regexp"

	authing "github.com/Authing/authing-go-sdk"
	prettyjson "github.com/hokaccha/go-prettyjson"
	"github.com/kelvinji2009/graphql"
)

const (
	clientID  = "5adb75e03055230001023b26"
	appSecret = "e683d18f9d597317d43d7a6522615b9d"
)

func main() {
    // ---User Endpoint
	client := authing.NewClient(clientID, appSecret, false)
	// Enable debug info for graphql client, just comment it if you want to disable the debug info
	client.Client.Log = func(s string) {
		b := []byte(s)
		pj, _ := prettyjson.Format(b)
		fmt.Println(string(pj))
	}

	// >>>Graphql Mutation: register
	input := authing.UserRegisterInput{
		Email:            graphql.String("[email protected]"),
		Password:         graphql.String("password"),
		RegisterInClient: graphql.String(clientID),
	}

	m, err := client.Register(&input)
	if err != nil {
		log.Println(">>>>Register failed: " + err.Error())
	} else {
		printJSON(m)
	}

    // ---OAuth Endpoint
	oauthClient := authing.NewOauthClient(clientID, appSecret, false)
	// Enable debug info for graphql client, just comment it if you want to disable the debug info
	oauthClient.Client.Log = func(s string) {
		b := []byte(s)
		pj, _ := prettyjson.Format(b)
		fmt.Println(string(pj))
	}

	// >>>>Graphql Query: Read OAuth List
	readOauthListQueryParameter := authing.ReadOauthListQueryParameter{
		ClientID:   graphql.String(clientID),
		DontGetURL: graphql.Boolean(false),
	}

	q, err := oauthClient.ReadOauthList(&readOauthListQueryParameter)
	if err != nil {
		log.Println(">>>>Read OAuth List failed: " + err.Error())
	} else {
		printJSON(q)
	}

}

// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
func printJSON(v interface{}) {
	w := json.NewEncoder(os.Stdout)
	w.SetIndent("", "\t")
	err := w.Encode(v)
	if err != nil {
		panic(err)
	}
}

Usages And Examples

Precondition

  1. Please register Authing account on the official site.Authing Register/Login
  2. Read the official developer document.Authing Developer Docs
  3. Create new application following the guide and save the clientID and appSecert from the Dashboard.Create New Application

User Endpoint

Please create a user endpoint client first.Then you can do a series of operations for user.

client := authing.NewClient(clientID, appSecret, false)
// Enable debug info for graphql client, just comment it if you want to disable the debug info
client.Client.Log = func(s string) { log.Println(s) }

Register a new user

input := authing.UserRegisterInput{
	Email:            graphql.String("[email protected]"),
	Password:         graphql.String("password"),
	RegisterInClient: graphql.String(clientID),
}

m, err := client.Register(&input)
if err != nil {
	log.Println(">>>>Register failed: " + err.Error())
} else {
	printJSON(m)
}

User Login

loginInput := authing.UserLoginInput{
	Email:            graphql.String("[email protected]"),
	Password:         graphql.String("password!"),
	RegisterInClient: graphql.String(clientID),
}

m, err := client.Login(&loginInput)
if err != nil {
	log.Println(">>>>Login failed: " + err.Error())
} else {
	printJSON(m)
}

userID := string(m.Login.ID) 

Check Login Status

q, err := client.CheckLoginStatus()
if err != nil {
	log.Println(">>>>Check login status failed: " + err.Error())
} else {
	printJSON(q)
}

Query User Information

p := authing.UserQueryParameter{
	ID:               graphql.String("5ae3d830f0db4b000117a95e"),
	RegisterInClient: graphql.String(clientID),
}

q, err := client.User(&p)
if err != nil {
	log.Println(">>>>Query user failed: " + err.Error())
} else {
	printJSON(q)
}

Query All Users

p := authing.UsersQueryParameter{
	RegisterInClient: graphql.String(clientID),
	Page:             graphql.Int(1),
	Count:            graphql.Int(10),
}

q, err := client.Users(&p)
if err != nil {
	log.Println(">>>>Query users failed: " + err.Error())
} else {
	printJSON(q)
}

Remove User(s)

removeUsersInput := authing.RemoveUsersInput{
	IDs:              []graphql.String{"111", "222"}, // NOTE: Please use your real user IDs
	RegisterInClient: graphql.String(clientID),
	// Operator should be your `Authing.cn` account ID
	// Operator:         graphql.String("5adb75be3055230001023b20"), // no more needed
}

// UserID Validation
for i, id := range removeUsersInput.IDs {
	re := regexp.MustCompile("^[0-9a-fA-F]{24}$")

	if !re.MatchString(string(id)) {
		log.Fatalf(">>>> user ID is invalid ,index: %d, id: %s", i, id)
	}
}

m, err := client.RemoveUsers(&removeUsersInput)
if err != nil {
	log.Println(">>>>Remove users failed: " + err.Error())
} else {
	printJSON(m)
}

Update User Information

userUpdateInput := authing.UserUpdateInput{
	ID:               graphql.String("5ae3d830f0db4b000117a95e"), // Mandotory in struct
	Username:         graphql.String("kelvinji2009x"),
	Nickname:         graphql.String("Sicario13th"),
	Phone:            graphql.String("18665308994"),
	RegisterInClient: graphql.String(clientID),
}

m, err := client.UpdateUser(&userUpdateInput)
if err != nil {
	log.Println(">>>>Update user failed: " + err.Error())
} else {
	printJSON(m)
}

Send Verify Email

sendVerifyEmailInput := authing.SendVerifyEmailInput{
	Email:  graphql.String("[email protected]"),
	Client: graphql.String(clientID),
}

err := client.SendVerifyEmail(&sendVerifyEmailInput)
if err != nil {
	log.Println(">>>>Send verify email failed: " + err.Error())
}

Send Reset Password Email

sendResetPasswordEmailInput := authing.SendResetPasswordEmailInput{
	Client: graphql.String(clientID),
	Email:  graphql.String("[email protected]"),
}

err := client.SendResetPasswordEmail(&sendResetPasswordEmailInput)
if err != nil {
	log.Println(">>>>Send reset password email failed: " + err.Error())
}

Verify Reset Password Verify-Code

verifyResetPasswordVerifyCodeInput := authing.VerifyResetPasswordVerifyCodeInput{
	Client:     graphql.String(clientID),
	Email:      graphql.String("[email protected]"),
	VerifyCode: graphql.String("7670"),
}

err := client.VerifyResetPasswordVerifyCode(&verifyResetPasswordVerifyCodeInput)
if err != nil {
	log.Println(">>>>Verify reset passwod verify code failed: " + err.Error())
}

Change Password

changePasswordInput := authing.ChangePasswordInput{
	Client:     graphql.String(clientID),
	Email:      graphql.String("[email protected]"),
	VerifyCode: graphql.String("7670"),
	Password:   graphql.String("password!"),
}

err := client.ChangePassword(&changePasswordInput)
if err != nil {
	log.Println(">>>>Change password failed: " + err.Error())
}

OAuth Endpoint

Please create an oauth endpoint client first.

oauthClient := authing.NewOauthClient(clientID, appSecret, false)
// Enable debug info for graphql client, just comment it if you want to disable the debug info
oauthClient.Client.Log = func(s string) { log.Println(s) }

Read OAuth List

readOauthListQueryParameter := authing.ReadOauthListQueryParameter{
	ClientID:   graphql.String(clientID),
	DontGetURL: graphql.Boolean(false),
}

q, err := oauthClient.ReadOauthList(&readOauthListQueryParameter)
if err != nil {
	log.Println(">>>>Read OAuth List failed: " + err.Error())
} else {
	printJSON(q)
}

TODO

  • More detailed API usages and documents
  • Travis CI support

Thanks

Go GraphQL Client

Simple low-level GraphQL HTTP client for Go

authing-go-sdk's People

Contributors

kelvinji2009 avatar

Watchers

 avatar

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.