Skip to content

Commit

Permalink
enable nostr nip-05 login
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHatGarKeineNode committed Aug 21, 2023
1 parent 45b1b66 commit ddb79ae
Show file tree
Hide file tree
Showing 18 changed files with 3,772 additions and 297 deletions.
72 changes: 72 additions & 0 deletions app/Http/Livewire/Auth/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Http\Livewire\Auth;

use App\Models\Team;
use App\Models\User;
use Livewire\Component;

class Login extends Component
{
public array $userProfile = [];

public function rules()
{
return [
'userProfile.npub' => 'required|string',
'userProfile.pubkey' => 'required|string',

'userProfile.banner' => 'required|string',
'userProfile.image' => 'required|string',

'userProfile.name' => 'required|string',
'userProfile.username' => 'required|string',
'userProfile.website' => 'required|string',
'userProfile.about' => 'required|string',
'userProfile.displayName' => 'required|string',
'userProfile.lud16' => 'required|string',
'userProfile.nip05' => 'required|string',
];
}

public function updatedUserProfile($value)
{
if ($value['npub']) {
$firstUser = User::query()->where('nostr', $value['npub'])->first();
if ($firstUser) {
auth()->login($firstUser, true);

return to_route('welcome');
} else {
$fakeName = str()->random(10);
// create User
$user = User::create([
'is_lecturer' => true,
'name' => $fakeName,
'email' => str($fakeName)->slug() . '@portal.einundzwanzig.space',
'email_verified_at' => now(),
'lnbits' => [
'read_key' => null,
'url' => null,
'wallet_id' => null,
],
'nostr' => $value['npub'],
]);
$user->ownedTeams()
->save(Team::forceCreate([
'user_id' => $user->id,
'name' => $fakeName . "'s Team",
'personal_team' => true,
]));
auth()->login($user, true);

return to_route('welcome');
}
}
}

public function render()
{
return view('livewire.auth.login')->layout('layouts.guest');
}
}
44 changes: 44 additions & 0 deletions app/Http/Livewire/Auth/LoginWithNDK.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Livewire\Auth;

use App\Models\User;
use Livewire\Component;

class LoginWithNDK extends Component
{
public $existingAccount = false;

public array $userProfile = [];

public function rules()
{
return [
'userProfile.npub' => 'required|string',
'userProfile.pubkey' => 'required|string',

'userProfile.banner' => 'required|string',
'userProfile.image' => 'required|string',

'userProfile.name' => 'required|string',
'userProfile.username' => 'required|string',
'userProfile.website' => 'required|string',
'userProfile.about' => 'required|string',
'userProfile.displayName' => 'required|string',
'userProfile.lud16' => 'required|string',
'userProfile.nip05' => 'required|string',
];
}

public function updatedUserProfile($value)
{
if (User::query()->where('nostr', $value['npub'])->exists()) {
$this->existingAccount = true;
}
}

public function render()
{
return view('livewire.auth.login-with-n-d-k')->layout('layouts.guest');
}
}
2 changes: 1 addition & 1 deletion app/Http/Livewire/BookCase/Form/OrangePillForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function mount()
public function save()
{
if (!auth()->check()) {
return to_route('auth.ln');
return to_route('auth.login');
}
$this->validate();
$this->orangePill->save();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/Profile/Meetups.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function rules()
public function mount()
{
if (!auth()->user()) {
return to_route('auth.ln');
return to_route('auth.login');
}

$this->meetups = Meetup::query()
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Authenticate extends Middleware
*/
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : route('auth.ln');
return $request->expectsJson() ? null : route('auth.login');
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@alpinejs/collapse": "^3.10.5",
"@alpinejs/focus": "^3.11.1",
"@alpinejs/intersect": "^3.11.1",
"@nostr-dev-kit/ndk": "^0.8.20-1",
"@tailwindcss/forms": "^0.5.2",
"@tailwindcss/line-clamp": "^0.4.2",
"@tailwindcss/typography": "^0.5.0",
Expand Down
91 changes: 91 additions & 0 deletions public/img/nostr.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import Alpine from 'alpinejs'
import collapse from '@alpinejs/collapse'
import intersect from '@alpinejs/intersect'
import focus from '@alpinejs/focus'
import NDK, { NDKNip07Signer, NDKEvent } from "@nostr-dev-kit/ndk"

window.Alpine = Alpine
window.NDK = NDK
window.NDKNip07Signer = NDKNip07Signer
window.NDKEvent = NDKEvent

Alpine.plugin(collapse)
Alpine.plugin(intersect)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
@endif

<div class="flex items-center justify-left mt-4">
<x-button href="{{ route('auth.ln') }}" primary icon="lightning-bolt">LNURL-Auth</x-button>
<x-button href="{{ route('auth.login') }}" primary icon="lightning-bolt">LNURL-Auth</x-button>
</div>
</form>
</x-jet-authentication-card>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
</div>--}}

<div class="flex items-center justify-left mt-4">
<x-button href="{{ route('auth.ln') }}" primary icon="lightning-bolt">LNURL-Auth</x-button>
<x-button href="{{ route('auth.login') }}" primary icon="lightning-bolt">Login</x-button>
</div>
</form>
</x-jet-authentication-card>
Expand Down
15 changes: 8 additions & 7 deletions resources/views/livewire/auth/ln-url-auth.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</div>
</div>

@if(app()->environment('local'))
@if(false && app()->environment('local'))
<div class="flex items-center justify-end mt-4">

<x-button icon="login" secondary class="ml-4" wire:click="switchToEmailLogin">
Expand Down Expand Up @@ -100,6 +100,13 @@ class="leading-6 text-blue-400 bg-transparent cursor-pointer"
style="text-decoration: none; list-style: outside;"
>Balance of Satoshis</a
>,
<a
target="_blank"
href="https://www.walletofsatoshi.com/"
class="leading-6 text-blue-400 bg-transparent cursor-pointer"
style="text-decoration: none; list-style: outside;"
>Wallet of Satoshi</a
>,
<a
target="_blank" href="https://blixtwallet.github.io"
class="leading-6 text-blue-400 bg-transparent cursor-pointer"
Expand Down Expand Up @@ -208,12 +215,6 @@ class="leading-6 text-blue-400 bg-transparent cursor-pointer"
</td>

</div>

<div class="pt-12">
<p class="text-xs">{{ __('Zeus bug:') }} <a target="_blank"
href="https://github.com/ZeusLN/zeus/issues/1045">https://github.com/ZeusLN/zeus/issues/1045</a>
</p>
</div>
</div>
<div wire:poll="checkAuth" wire:key="checkAuth"></div>
</x-jet-authentication-card>
52 changes: 52 additions & 0 deletions resources/views/livewire/auth/login-with-n-d-k.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<x-jet-authentication-card>

<x-slot name="logo">
<x-jet-authentication-card-logo/>
</x-slot>

<div>
<div
wire:ignore
x-data="{
userProfile: @entangle('userProfile'),
init() {
const nip07signer = new window.NDKNip07Signer();
const ndk = new window.NDK({
explicitRelayUrls: ['wss://nostr.codingarena.de'],
signer: nip07signer
});
ndk.connect();
},
login() {
nip07signer.user().then(async (user) => {
if (!!user.npub) {
console.log('user pub: ' + user.npub);
const ndkUser = ndk.getUser({
npub: user.npub,
});
await ndkUser.fetchProfile();
console.log(ndkUser);
this.userProfile = ndkUser.profile;
}
});
}
}"
>

<div class="space-y-6" x-init="init()">
<x-button x-show="!userProfile.npub" primary label="NIP-07 Login" icon="login" @click="login()"/>
<p x-text="userProfile.npub"></p>
<p x-text="userProfile.about"></p>
<img :src="userProfile.image" alt="image"/>
</div>
</div>

@if($existingAccount)
<div class="mt-12 text-red-500 space-y-6">
<p>Es existiert ein Account mit dem npub {{ $userProfile['npub'] }}</p>
</div>
@endif
</div>

</x-jet-authentication-card>
Loading

0 comments on commit ddb79ae

Please sign in to comment.