Skip to content

Commit e72464f

Browse files
Random avatar generator for profiles #154 (#207)
Co-authored-by: Sanchit Bajaj <55249639+Sanchitbajaj02@users.noreply.github.com>
1 parent 9c5136e commit e72464f

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

src/backend/auth.api.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { account, db, ID, palettegramDB, usersCollection, Query } from "./appwrite.config";
4+
import { generateAvatar } from './avatarGenerator';
45

56
/**
67
* @description Register the user into the database
@@ -9,6 +10,18 @@ import { account, db, ID, palettegramDB, usersCollection, Query } from "./appwri
910
*/
1011
const registerUser = async (userData: any) => {
1112
try {
13+
14+
// console.log("register: ", userData.email, userData.password, userData.fullName);
15+
if (userData.password != userData.confirmpassword) {
16+
throw Error("not matching");
17+
}
18+
const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; //it will check that password must contain atleast one digit ,atleast one alphabet , atleast one special character and must be of atleast length 8
19+
20+
if (!passwordRegex.test(userData.password)) {
21+
throw Error("password is not strong");
22+
}
23+
const avatar = generateAvatar(userData.fullName);
24+
1225
const authResponse = await account.create(
1326
ID.unique(),
1427
userData.email,
@@ -191,12 +204,14 @@ const saveDataToDatabase = async (session: any) => {
191204
try {
192205
let username = session.email.split("@")[0];
193206
console.log(username);
207+
const avatar = generateAvatar(session.name);
194208
const resp = await db.createDocument(palettegramDB, usersCollection, ID.unique(), {
195209
email: session.email,
196210
fullName: session.name,
197211
isVerified: session.emailVerification,
198212
accountId: session.$id,
199213
username: username,
214+
avatarURL: avatar,
200215
});
201216
if (!resp) {
202217
throw new Error("Database not working");

src/backend/avatarGenerator.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const generateAvatar = (name: string, variant: string = 'marble', size: number = 120, colors: string[] = []): string => {
2+
const encodedName = encodeURIComponent(name);
3+
const baseUrl = 'https://source.boringavatars.com/';
4+
5+
let url = `${baseUrl}${variant}/${size}/${encodedName}`;
6+
7+
if (colors.length > 0) {
8+
const encodedColors = colors.map(color => encodeURIComponent(color)).join(',');
9+
url += `?colors=${encodedColors}`;
10+
}
11+
12+
return url;
13+
};

src/components/pages/user/index.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ export default function User({ userId }: { userId: string }) {
4848
className="object-center object-cover rounded-md"
4949
/>
5050
</div>
51-
<section className="-mt-20 px-2">
52-
<div className="flex justify-between items-end">
53-
<Image
54-
src="/assets/user.png"
55-
alt="user"
56-
width={135}
57-
height={135}
58-
className=" border-4 border-white dark:border-slate-800 rounded-full object-contain"
59-
/>
51+
52+
<section className="-mt-20 px-3">
53+
<div className="flex justify-between items-end ">
54+
<Image
55+
src={user && user?.documents[0].avatarURL}
56+
alt="user"
57+
width={125}
58+
height={125}
59+
className="border-4 border-white dark:border-slate-800 rounded-full object-contain "
60+
/>
61+
6062
<div className="h-fit flex gap-4">
6163
<Mail className="h-7 w-7 sm:h-9 sm:w-9 border-2 p-1 rounded-full text-slate-700 dark:text-slate-400 border-slate-700 dark:border-slate-400" />
6264
<ButtonLong href="#" size="normal">

src/redux/reducers/authReducer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const initialState: userDetail = {
1010
email: "",
1111
createdAt: "",
1212
isVerified: false,
13+
avatarURL: "",
1314
},
1415
error: false,
1516
loading: false,

src/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type userDetail = {
2525
email: string;
2626
createdAt: string;
2727
isVerified: boolean;
28+
avatarURL: string;
2829
};
2930
error: boolean;
3031
loading: boolean;

0 commit comments

Comments
 (0)