godash/components/user.templ

59 lines
2 KiB
Text
Raw Normal View History

2024-03-25 20:31:51 +01:00
package components
import (
2024-04-03 17:55:48 +02:00
"crypto/md5"
"fmt"
"strings"
"html/template"
"github.com/zitadel/oidc/v3/pkg/oidc"
openid "github.com/zitadel/zitadel-go/v3/pkg/authentication/oidc"
2024-03-25 20:31:51 +01:00
)
2024-04-03 17:55:48 +02:00
var GravatarTemplate = template.Must(template.New("gravatar").Parse("<div class=\"avatar rounded w-10 h-10 bg-contain bg-center bg-origin-content bg-no-repeat opacity-90\" style=\"background-image: url({{ . }})\"></div>"))
func GetGravatarURL(email string, size uint) string {
email = strings.TrimSpace(strings.ToLower(email))
hash := md5.Sum([]byte(email))
gravatarURL := fmt.Sprintf("https://www.gravatar.com/avatar/%x?s=%d", hash, size)
return string(templ.URL(gravatarURL))
}
templ User(authCtx *openid.UserInfoContext[*oidc.IDTokenClaims, *oidc.UserInfo]) {
2024-03-25 20:31:51 +01:00
<div
x-data="{
open: false,
toggle() {
if (this.open) return this.close();
this.open = true;
},
close(focusAfter) {
if (! this.open) return;
this.open = false;
}
}"
class="relative"
>
<div x-on:click="toggle()" class="flex items-center gap-2 hover:cursor-pointer opacity-80 hover:opacity-100 transition-opacity">
2024-04-03 15:34:00 +02:00
<div class="hidden md:flex flex-col items-end">
<div class="text-secondary text-sm font-bold whitespace-nowrap">{ authCtx.UserInfo.Name }</div>
<div class="text-gray-500 text-xs whitespace-nowrap">{ authCtx.UserInfo.Email }</div>
</div>
2024-04-03 17:55:48 +02:00
@templ.FromGoHTML(GravatarTemplate, GetGravatarURL(authCtx.UserInfo.Email, 100))
</div>
2024-03-25 20:31:51 +01:00
<ul
x-show="open"
x-transition.origin.top.left
2024-03-25 20:31:51 +01:00
x-on:click.outside="close($refs.button)"
style="display: none;"
class="z-50 absolute right-0 mt-2 menu bg-base-200 rounded-box"
>
2024-04-03 15:34:00 +02:00
<li class="md:hidden menu-title whitespace-nowrap">{ authCtx.UserInfo.Name }</li>
<li class="md:hidden menu-title whitespace-nowrap">{ authCtx.UserInfo.Email }</li>
2024-03-25 20:31:51 +01:00
<li>
<a href="/auth/logout"><span class="icon-[bi--box-arrow-left]"></span> Logout</a>
2024-03-25 20:31:51 +01:00
</li>
</ul>
</div>
}