appwrite config
This commit is contained in:
parent
f20e07c952
commit
962d9c7341
66
lib/appwrite.ts
Normal file
66
lib/appwrite.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import {Account, Avatars, Client, OAuthProvider} from "react-native-appwrite";
|
||||||
|
import * as Linking from "expo-linking"
|
||||||
|
import {openAuthSessionAsync} from "expo-web-browser";
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
platform: 'com.placebomk.mobilemk',
|
||||||
|
endpoint: process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT,
|
||||||
|
projectId: process.env.EXPO_PUBLIC_APPWRITE_PROJECT_ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const client = new Client();
|
||||||
|
|
||||||
|
client
|
||||||
|
.setEndpoint(config.endpoint!)
|
||||||
|
.setProject(config.projectId!)
|
||||||
|
.setPlatform(config.platform!)
|
||||||
|
|
||||||
|
export const avatar = new Avatars(client);
|
||||||
|
export const account = new Account(client);
|
||||||
|
|
||||||
|
|
||||||
|
export async function login (){
|
||||||
|
// Your code here to authenticate the user and return their JWT token.
|
||||||
|
try {
|
||||||
|
const redirectUri = Linking.createURL('/');
|
||||||
|
const response = await account.createOAuth2Token(OAuthProvider.Google, redirectUri);
|
||||||
|
|
||||||
|
if(!response) throw new Error('failed to login');
|
||||||
|
|
||||||
|
const browserResult = await openAuthSessionAsync(
|
||||||
|
response.toString(),
|
||||||
|
redirectUri
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!browserResult.type || browserResult.type !== 'success') {
|
||||||
|
throw new Error('failed to log in');
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = new URL(browserResult.url);
|
||||||
|
|
||||||
|
const secret = url.searchParams.get('secret')?.toString();
|
||||||
|
const userId = url.searchParams.get('userId')?.toString();
|
||||||
|
|
||||||
|
if (!secret ||!userId) {
|
||||||
|
throw new Error('failed to authenticate user');
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = await account.createSession(userId, secret);
|
||||||
|
if (!session) throw new Error('failed to create session');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error("Failed to authenticate user:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function logOut(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await account.deleteSession(sessionId: 'current');
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to log out user:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user