Compare commits
1 Commits
4ca2bae5a2
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
258d2b560c |
@@ -1,4 +1,4 @@
|
||||
The MIT License (MIT) Copyright (c) 2023 - present, Efim Nefedov
|
||||
The MIT License (MIT) Copyright (c) 2022 - present, Efim Nefedov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
@@ -174,18 +174,6 @@ cool
|
||||
oh, but if i'm using nginx i'll need my own certificate, that makes sence
|
||||
*** maybe things are ok?
|
||||
let's try to plaintext deploy?
|
||||
*** quoting of the '' in multiline string
|
||||
https://nixos.org/manual/nix/stable/language/values.html
|
||||
*** not accessible still
|
||||
sudo journalctl -u nginx --since "1 day ago"
|
||||
*** oh, i forgot to add subname in gandi ui
|
||||
now works
|
||||
*** now i need a way to pass in the hostname
|
||||
because front-end is setting up js 'new PocketBase' with 127.0.0.1 connection
|
||||
*** adding a custom flag:
|
||||
https://github.com/pocketbase/pocketbase/discussions/1900
|
||||
|
||||
|
||||
|
||||
** TODO add docker image from nix
|
||||
*** TODO add cli for port and host
|
||||
|
||||
47
flake.nix
47
flake.nix
@@ -42,8 +42,8 @@
|
||||
nixosModules.auth-pocketbase-attempt = { config, pkgs, ... }:
|
||||
let
|
||||
cfg = config.services.${pname};
|
||||
lib = nixpkgs.lib;
|
||||
shortName = "pb-auth-example-app";
|
||||
lib = pkgs.lib;
|
||||
shortName = "pb-auth-example-group";
|
||||
in {
|
||||
options.services.${pname} = {
|
||||
enable = lib.mkEnableOption
|
||||
@@ -71,20 +71,16 @@
|
||||
"Whether pocketbase should serve on https and issue own certs. Main case for true - when not under nginx";
|
||||
};
|
||||
};
|
||||
config = let
|
||||
username = "${shortName}-user";
|
||||
groupname = "${shortName}-group";
|
||||
in lib.mkIf cfg.enable {
|
||||
users.groups."${groupname}" = { };
|
||||
users.users."${username}" = {
|
||||
isNormalUser = true; # needed to allow for home dir
|
||||
group = "${groupname}";
|
||||
config = lib.mkIf cfg.enable {
|
||||
users.groups."${shortName}-group" = { };
|
||||
users.users."${shortName}-user" = {
|
||||
isSystemUser = true;
|
||||
group = "${shortName}-group";
|
||||
};
|
||||
systemd.services.${shortName} = let
|
||||
protocol = if cfg.usePbTls then "https" else "http";
|
||||
serverHost = if cfg.useNginx then "127.0.0.1" else cfg.host;
|
||||
serveCliArg =
|
||||
"--${protocol} ${serverHost}:${toString cfg.port}";
|
||||
servedAddress = "${protocol}://${serverHost}:${cfg.port}";
|
||||
in {
|
||||
description = "Exercise app ${pname}";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
@@ -93,31 +89,12 @@
|
||||
startLimitBurst = 10;
|
||||
serviceConfig = {
|
||||
ExecStart =
|
||||
"${packages.auth-pocketbase-attempt}/bin/${pname} serve ${serveCliArg} --dir=/home/${
|
||||
"${username}"
|
||||
"${packages.auth-pocketbase-attempt}/bin/${pname} serve ${servedAddress} --dir=/home/${
|
||||
config.users.users."${shortName}-user"
|
||||
}";
|
||||
Restart = "on-failure";
|
||||
User = "${username}";
|
||||
Group = "${groupname}";
|
||||
};
|
||||
};
|
||||
services.nginx = lib.mkIf cfg.useNginx {
|
||||
virtualHosts.${cfg.host}.locations."/" = {
|
||||
proxyPass =
|
||||
"http://127.0.0.1:${toString cfg.port}";
|
||||
# taken from https://pocketbase.io/docs/going-to-production/
|
||||
extraConfig = ''
|
||||
# check http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
|
||||
proxy_set_header Connection ''';
|
||||
proxy_http_version 1.1;
|
||||
proxy_read_timeout 360s;
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
'';
|
||||
# TODO doesn't include tls sadly
|
||||
User = "${shortName}-user";
|
||||
Group = "${shortName}-group";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@@ -23,6 +23,10 @@ func AddCookieSessionMiddleware(app *pocketbase.PocketBase) {
|
||||
|
||||
// fires for every auth collection
|
||||
app.OnRecordAuthRequest().Add(func(e *core.RecordAuthEvent) error {
|
||||
log.Println(e.HttpContext)
|
||||
log.Println(e.Record)
|
||||
log.Println(e.Token)
|
||||
log.Println(e.Meta)
|
||||
e.HttpContext.SetCookie(&http.Cookie{
|
||||
Name: AuthCookieName,
|
||||
Value: e.Token,
|
||||
@@ -35,6 +39,9 @@ func AddCookieSessionMiddleware(app *pocketbase.PocketBase) {
|
||||
return nil
|
||||
})
|
||||
app.OnAdminAuthRequest().Add(func(e *core.AdminAuthEvent) error {
|
||||
log.Println(e.HttpContext)
|
||||
log.Println(e.Admin)
|
||||
log.Println(e.Token)
|
||||
e.HttpContext.SetCookie(&http.Cookie{
|
||||
Name: AuthCookieName,
|
||||
Value: e.Token,
|
||||
@@ -67,6 +74,14 @@ func loadAuthContextFromCookie(app core.App) echo.MiddlewareFunc {
|
||||
if err == nil && admin != nil {
|
||||
// "authenticate" the admin
|
||||
c.Set(apis.ContextAdminKey, admin)
|
||||
someData := struct {
|
||||
username string
|
||||
email string
|
||||
} {
|
||||
admin.Email,
|
||||
admin.Created.String(),
|
||||
}
|
||||
fmt.Printf("triggering the middlewar for cookie %v and err %v\n", someData, err)
|
||||
}
|
||||
|
||||
case tokens.TypeAuthRecord:
|
||||
@@ -77,6 +92,15 @@ func loadAuthContextFromCookie(app core.App) echo.MiddlewareFunc {
|
||||
if err == nil && record != nil {
|
||||
// "authenticate" the app user
|
||||
c.Set(apis.ContextAuthRecordKey, record)
|
||||
someData := struct {
|
||||
username string
|
||||
email string
|
||||
} {
|
||||
record.Username(),
|
||||
record.Email(),
|
||||
}
|
||||
fmt.Printf("triggering the middlewar for cookie %v and err %v\n", someData, err)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,8 @@ func getIndexPageRoute(app *pocketbase.PocketBase) func(*core.ServeEvent) error
|
||||
record := info.AuthRecord // nil if not authenticated as regular auth record
|
||||
|
||||
isGuest := admin == nil && record == nil
|
||||
coolMessage := fmt.Sprintf("got admin %v and record %v. is guest: %t", admin, record, isGuest)
|
||||
fmt.Print(coolMessage)
|
||||
|
||||
username := ""
|
||||
switch {
|
||||
@@ -63,6 +65,7 @@ func getIndexPageRoute(app *pocketbase.PocketBase) func(*core.ServeEvent) error
|
||||
oauthProviderNames = append(oauthProviderNames, name)
|
||||
}
|
||||
}
|
||||
fmt.Printf(">> enabled providers names %+v\n", oauthProviderNames)
|
||||
|
||||
indexPageData := struct {
|
||||
IsGuest, IsAdmin bool
|
||||
|
||||
@@ -46,8 +46,7 @@
|
||||
</dialog>
|
||||
<script defer type="text/javascript">
|
||||
async function callOauth(providerName) {
|
||||
const baseUrl = window.location.protocol + "//" + window.location.host;
|
||||
const pb = new PocketBase(baseUrl);
|
||||
const pb = new PocketBase("http://127.0.0.1:8090");
|
||||
|
||||
// This method initializes a one-off realtime subscription and will
|
||||
// open a popup window with the OAuth2 vendor page to authenticate.
|
||||
|
||||
Reference in New Issue
Block a user