# 🖼️ Avatar Özelliği Eklendi!
## ✨ Yeni Özellikler
### User Modeline Avatar Eklendi
```go
type User struct {
ID uuid.UUID `json:"id"`
UserName string `json:"username"`
Email string `json:"email"`
Avatar string `json:"avatar,omitempty"` // ✅ YENİ!
// ...diğer alanlar
}
```
### SocialAccount Modeline Avatar ve Name Eklendi
```go
type SocialAccount struct {
ID uuid.UUID `json:"id"`
UserID uuid.UUID `json:"user_id"`
Provider string `json:"provider"`
ProviderID string `json:"provider_id"`
Email string `json:"email"`
Name string `json:"name,omitempty"` // ✅ YENİ!
AvatarURL string `json:"avatar_url,omitempty"` // ✅ YENİ!
// ...
}
```
---
## 🎯 Nasıl Çalışıyor?
### 1. OAuth ile Giriş (Google/GitHub)
Kullanıcı OAuth ile giriş yaptığında:
1. ✅ Provider'dan avatar URL alınır (`gothUser.AvatarURL`)
2. ✅ User tablosuna `avatar` field'ına kaydedilir
3. ✅ SocialAccount tablosuna `avatar_url` ve `name` kaydedilir
4. ✅ Her giriş yaptığında avatar güncel değilse güncellenir
**Örnek Response:**
```json
{
"access_token": "eyJhbGci...",
"refresh_token": "eyJhbGci...",
"user": {
"id": "uuid",
"email": "user@gmail.com",
"username": "John Doe",
"avatar": "https://lh3.googleusercontent.com/a/ACg8ocK...",
"email_verified": true,
"roles": [{"name": "user"}],
"social_accounts": [
{
"provider": "google",
"name": "John Doe",
"avatar_url": "https://lh3.googleusercontent.com/a/ACg8ocK..."
}
]
}
}
```
### 2. Manuel Avatar Güncelleme (Admin)
Admin kullanıcılar avatar URL'sini manuel olarak güncelleyebilir:
```bash
curl -X PUT http://localhost:8080/v1/admin/users/USER_ID \
-H "Authorization: Bearer ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"avatar": "https://example.com/avatars/user123.jpg"
}'
```
---
## 📋 Avatar Kaynakları
### Google OAuth
```
https://lh3.googleusercontent.com/a/ACg8ocK...
```
### GitHub OAuth
```
https://avatars.githubusercontent.com/u/1234567?v=4
```
### Manuel Upload (Gelecekte)
```
https://yourdomain.com/uploads/avatars/user-uuid.jpg
```
---
## 🔄 API Response'larında Avatar
### GET /v1/auth/me
```json
{
"id": "uuid",
"email": "user@example.com",
"username": "username",
"avatar": "https://lh3.googleusercontent.com/a/...",
"email_verified": true,
"roles": [{"name": "user"}]
}
```
### GET /v1/admin/users
```json
{
"users": [
{
"id": "uuid",
"email": "user@example.com",
"username": "username",
"avatar": "https://lh3.googleusercontent.com/a/...",
"social_accounts": [
{
"provider": "google",
"name": "John Doe",
"avatar_url": "https://lh3.googleusercontent.com/a/..."
}
]
}
]
}
```
### POST /v1/auth/login (OAuth Callback)
```json
{
"access_token": "...",
"refresh_token": "...",
"user": {
"id": "uuid",
"email": "user@gmail.com",
"username": "John Doe",
"avatar": "https://lh3.googleusercontent.com/a/...",
"roles": [{"name": "user"}]
}
}
```
---
## 🧪 Test Örnekleri
### Test 1: Google ile Giriş Yap
```bash
# 1. OAuth URL'sine git
http://localhost:8080/v1/auth/google
# 2. Google'da izin ver
# 3. Callback'te avatar ile kullanıcı dönecek
{
"access_token": "...",
"user": {
"avatar": "https://lh3.googleusercontent.com/...",
"email": "user@gmail.com",
"username": "Your Name"
}
}
```
### Test 2: GitHub ile Giriş Yap
```bash
# 1. OAuth URL'sine git
http://localhost:8080/v1/auth/github
# 2. GitHub'da izin ver
# 3. Callback'te avatar ile kullanıcı dönecek
{
"access_token": "...",
"user": {
"avatar": "https://avatars.githubusercontent.com/u/...",
"email": "user@github.com",
"username": "githubusername"
}
}
```
### Test 3: Avatar Güncelleme (Admin)
```bash
TOKEN="admin_token_here"
USER_ID="user_uuid"
curl -X PUT "http://localhost:8080/v1/admin/users/$USER_ID" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"avatar": "https://example.com/new-avatar.jpg"
}'
```
**Response:**
```json
{
"message": "User updated successfully",
"user": {
"id": "uuid",
"email": "user@example.com",
"username": "username",
"avatar": "https://example.com/new-avatar.jpg",
"updated_at": "2026-02-04T..."
}
}
```
### Test 4: Kullanıcı Bilgilerini Getir
```bash
curl -X GET http://localhost:8080/v1/auth/me \
-H "Authorization: Bearer USER_TOKEN"
```
**Response:**
```json
{
"id": "uuid",
"email": "user@gmail.com",
"username": "John Doe",
"avatar": "https://lh3.googleusercontent.com/a/...",
"email_verified": true,
"created_at": "2026-02-04T...",
"social_accounts": [
{
"provider": "google",
"name": "John Doe",
"avatar_url": "https://lh3.googleusercontent.com/a/..."
}
],
"roles": [{"name": "user"}]
}
```
---
## 💻 Frontend Kullanımı
### React Örneği
```jsx
function UserAvatar({ user }) {
const defaultAvatar = 'https://ui-avatars.com/api/?name=' +
encodeURIComponent(user.username);
return (
{
e.target.src = defaultAvatar;
}}
/>
);
}
// Kullanım
function Header() {
const [user, setUser] = useState(null);
useEffect(() => {
fetch('http://localhost:8080/v1/auth/me', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
})
.then(res => res.json())
.then(data => setUser(data));
}, []);
return (