Skip to content

Commit

Permalink
Simplifying auth with Node js with the appName field
Browse files Browse the repository at this point in the history
  • Loading branch information
baptistegreve committed Sep 13, 2023
1 parent 7a66a28 commit 39516ed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export class Orbis {
}

/** The connect function will connect to an EVM wallet and create or connect to a Ceramic did */
async connect_v2({provider, chain = "ethereum", lit = false, oauth = null}) {
async connect_v2({provider, chain = "ethereum", lit = false, oauth = null, appName = null}) {
/** Save chain we are using in global state */
this.chain = chain;

Expand All @@ -329,7 +329,7 @@ export class Orbis {
let did;

/** Retrieve authMethod and address based on the provider and chain passed as a parameter */
let { authMethod, address } = await getAuthMethod(provider, chain);
let { authMethod, address } = await getAuthMethod(provider, chain, appName);

/** User is connecting with a web2 provider */
if(provider == 'oauth') {
Expand Down Expand Up @@ -1509,7 +1509,7 @@ export class Orbis {
}

else {
query = this.api.rpc("default_posts_08", {
query = this.api.rpc("default_posts_09", {
q_did: options?.did ? options.did : null,
q_tag: options?.tag ? options.tag : null,
q_only_master: options?.only_master ? options.only_master : false,
Expand All @@ -1518,7 +1518,9 @@ export class Orbis {
q_master: options?.master ? options.master : null,
q_reply_to: options?.reply_to ? options.reply_to : null,
q_include_child_contexts: options?.include_child_contexts ? options.include_child_contexts : false,
q_term: options?.term ? options.term : null
q_term: options?.term ? options.term : null,
q_is_reply: options?.is_reply ? options.is_reply : null,
q_is_repost: options?.is_repost ? options.is_repost : null,
}).range(page * limit, (page + 1) * limit - 1).order(options?.order_by ? options.order_by : 'timestamp', { ascending: ascending });
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orbisclub/orbis-sdk",
"version": "0.4.82",
"version": "0.4.87",
"description": "Official package to implement quickly an Orbis powered decentralized social layer within your application.",
"author": "Baptiste Grève",
"license": "ISC",
Expand Down
15 changes: 12 additions & 3 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Ed25519Provider } from 'key-did-provider-ed25519'
import { getResolver } from 'key-did-resolver'

/** Manage did:pkh */
import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'
import { EthereumWebAuth, EthereumNodeAuth, getAccountId } from '@didtools/pkh-ethereum'
import { SolanaWebAuth, getAccountIdByNetwork} from '@didtools/pkh-solana'
import { TezosWebAuth, getAccountId as getTzAccountId } from '@didtools/pkh-tezos';
import { StacksWebAuth, getAccountIdByNetwork as getStacksAccountId } from "@didtools/pkh-stacks";
Expand Down Expand Up @@ -186,7 +186,7 @@ export function checkVcOwnership(user_credentials, credential_required) {
}

/** Function to return the correct authMethod based on the provider and network used */
export async function getAuthMethod(provider, chain) {
export async function getAuthMethod(provider, chain, appName) {
let authMethod;
let address;
let accountId;
Expand All @@ -210,6 +210,7 @@ export async function getAuthMethod(provider, chain) {
/** Step 2: Check if user already has an active account on Orbis */
let defaultChain = "1";
address = addresses[0].toLowerCase();
console.log("Connecting with:", address);
accountId = await getAccountId(provider, address)

/** Check if the user trying to connect already has an existing did on Orbis */
Expand All @@ -228,8 +229,16 @@ export async function getAuthMethod(provider, chain) {

/** Step 2: Create an authMethod object using the address connected */
try {
authMethod = await EthereumWebAuth.getAuthMethod(provider, accountId);
if(appName) {
console.log("appName " + appName + " received, login with EthereumNodeAuth.");
/** Login with NodeAuth if an app name is passed as a parameter (might be used for React Native as well) */
authMethod = await EthereumNodeAuth.getAuthMethod(provider, accountId, appName);
} else {
/** Login with WebAuth if no app name passed */
authMethod = await EthereumWebAuth.getAuthMethod(provider, accountId);
}
} catch(e) {
console.log("Error creating Ethereum authMethod object for Ceramic:", e);
return {
status: 300,
error: e,
Expand Down

0 comments on commit 39516ed

Please sign in to comment.