Merge pull request #2 from ForFarmTeam/feature-authen

Add credential authenticaton, axios config to send token in header and hooks
This commit is contained in:
Sirin Puenggun 2025-02-14 00:17:00 +07:00 committed by GitHub
commit f3af16a06e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
35 changed files with 4322 additions and 2621 deletions

View File

@ -5,37 +5,44 @@ go 1.23.5
require (
github.com/danielgtaylor/huma/v2 v2.28.0
github.com/go-chi/chi/v5 v5.2.1
github.com/go-chi/jwtauth/v5 v5.3.2
github.com/go-chi/cors v1.2.1
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.7.2
github.com/joho/godotenv v1.5.1
github.com/lestrrat-go/jwx/v2 v2.1.3
github.com/pressly/goose/v3 v3.24.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
golang.org/x/crypto v0.31.0
)
require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.6 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -4,26 +4,32 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/danielgtaylor/huma/v2 v2.28.0 h1:W+hIT52MigO73edJNJWXU896uC99xSBWpKoE2PRyybM=
github.com/danielgtaylor/huma/v2 v2.28.0/go.mod h1:67KO0zmYEkR+LVUs8uqrcvf44G1wXiMIu94LV/cH2Ek=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-chi/jwtauth/v5 v5.3.2 h1:s+ON3ATyyMs3Me0kqyuua6Rwu+2zqIIkL0GCaMarwvs=
github.com/go-chi/jwtauth/v5 v5.3.2/go.mod h1:O4QvPRuZLZghl9WvfVaON+ARfGzpD2PBX/QY5vUz7aQ=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@ -36,52 +42,65 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
github.com/lestrrat-go/httprc v1.0.6 h1:qgmgIRhpvBqexMJjA/PmwSvhNk679oqD1RbovdCGW8k=
github.com/lestrrat-go/httprc v1.0.6/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
github.com/lestrrat-go/jwx/v2 v2.1.3 h1:Ud4lb2QuxRClYAmRleF50KrbKIoM1TddXgBrneT5/Jo=
github.com/lestrrat-go/jwx/v2 v2.1.3/go.mod h1:q6uFgbgZfEmQrfJfrCo90QcQOcXFMfbI/fO0NqRtvZo=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.24.1 h1:bZmxRco2uy5uu5Ng1MMVEfYsFlrMJI+e/VMXHQ3C4LY=
github.com/pressly/goose/v3 v3.24.1/go.mod h1:rEWreU9uVtt0DHCyLzF9gRcWiiTF/V+528DV+4DORug=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
@ -89,6 +108,10 @@ golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -10,6 +10,7 @@ import (
"github.com/danielgtaylor/huma/v2/adapters/humachi"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/forfarm/backend/internal/domain"
@ -48,6 +49,17 @@ func (a *api) Routes() *chi.Mux {
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Use(cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"https://*", "http://*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
config := huma.DefaultConfig("ForFarm Public API", "v1.0.0")
api := humachi.New(router, config)

View File

@ -11,10 +11,11 @@ import (
"github.com/forfarm/backend/internal/api"
"github.com/forfarm/backend/internal/cmdutil"
"github.com/forfarm/backend/internal/config"
)
func APICmd(ctx context.Context) *cobra.Command {
var port int = 8000
var port int = config.PORT
cmd := &cobra.Command{
Use: "api",
@ -29,6 +30,8 @@ func APICmd(ctx context.Context) *cobra.Command {
}
defer pool.Close()
logger.Info("connected to database")
api := api.NewAPI(ctx, logger, pool)
server := api.Server(port)

View File

@ -2,14 +2,15 @@ package cmd
import (
"context"
"os"
"github.com/forfarm/backend/internal/config"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
)
func Execute(ctx context.Context) int {
_ = godotenv.Load()
config.Load()
rootCmd := &cobra.Command{
Use: "forfarm",
@ -17,7 +18,7 @@ func Execute(ctx context.Context) int {
}
rootCmd.AddCommand(APICmd(ctx))
rootCmd.AddCommand(MigrateCmd(ctx, "pgx", os.Getenv("DATABASE_URL")))
rootCmd.AddCommand(MigrateCmd(ctx, "pgx", config.DATABASE_URL))
if err := rootCmd.Execute(); err != nil {
return 1

View File

@ -3,9 +3,9 @@ package cmdutil
import (
"context"
"fmt"
"os"
"time"
"github.com/forfarm/backend/internal/config"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
@ -15,7 +15,7 @@ func NewDatabasePool(ctx context.Context, maxConns int) (*pgxpool.Pool, error) {
maxConns = 1
}
url := fmt.Sprintf("%s?pool_max_conns=%d&pool_min_conns=%d", os.Getenv("DATABASE_URL"), maxConns, 2)
url := fmt.Sprintf("%s?pool_max_conns=%d&pool_min_conns=%d", config.DATABASE_URL, maxConns, 2)
config, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, err

View File

@ -0,0 +1,50 @@
package config
import (
"log"
"github.com/spf13/viper"
)
var (
PORT int
POSTGRES_USER string
POSTGRES_PASSWORD string
POSTGRES_DB string
DATABASE_URL string
GOOGLE_CLIENT_ID string
GOOGLE_CLIENT_SECRET string
GOOGLE_REDIRECT_URL string
JWT_SECRET_KEY string
)
func Load() {
viper.SetDefault("PORT", 8000)
viper.SetDefault("POSTGRES_USER", "postgres")
viper.SetDefault("POSTGRES_PASSWORD", "@Password123")
viper.SetDefault("POSTGRES_DB", "postgres")
viper.SetDefault("DATABASE_URL", "localhost")
viper.SetDefault("GOOGLE_CLIENT_ID", "google_client_id")
viper.SetDefault("GOOGLE_CLIENT_SECRET", "google_client_secret")
viper.SetDefault("JWT_SECRET_KEY", "jwt_secret_key")
viper.SetDefault("GOOGLE_REDIRECT_URL", "http://localhost:8000/auth/login/google")
viper.SetConfigFile(".env")
viper.AddConfigPath("../../.")
if err := viper.ReadInConfig(); err != nil {
log.Printf("Warning: Could not read config file: %v", err)
}
viper.AutomaticEnv()
PORT = viper.GetInt("PORT")
POSTGRES_USER = viper.GetString("POSTGRES_USER")
POSTGRES_PASSWORD = viper.GetString("POSTGRES_PASSWORD")
POSTGRES_DB = viper.GetString("POSTGRES_DB")
DATABASE_URL = viper.GetString("DATABASE_URL")
GOOGLE_CLIENT_ID = viper.GetString("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = viper.GetString("GOOGLE_CLIENT_SECRET")
GOOGLE_REDIRECT_URL = viper.GetString("GOOGLE_REDIRECT_URL")
JWT_SECRET_KEY = viper.GetString("JWT_SECRET_KEY")
}

View File

@ -2,6 +2,7 @@ package domain
import (
"context"
"errors"
"strings"
"time"
@ -27,7 +28,22 @@ func (u *User) NormalizedUsername() string {
func (u *User) Validate() error {
return validation.ValidateStruct(u,
validation.Field(&u.UUID, validation.Required),
validation.Field(&u.Username, validation.Length(3, 20)),
validation.Field(&u.Username, validation.By(func(value interface{}) error {
if value == nil {
return nil
}
if value == "" {
return nil
}
username, ok := value.(*string)
if !ok {
return errors.New("invalid type for username")
}
if len(*username) < 3 || len(*username) > 20 {
return errors.New("username length must be between 3 and 20")
}
return nil
})),
validation.Field(&u.Password, validation.Required, validation.Length(6, 100)),
validation.Field(&u.Email, validation.Required, is.Email),
)

View File

@ -1,13 +1,15 @@
package utilities
import (
"errors"
"time"
"github.com/forfarm/backend/internal/config"
"github.com/golang-jwt/jwt/v5"
)
// TODO: Change later
var secretKey = []byte("secret-key")
var deafultSecretKey = []byte(config.JWT_SECRET_KEY)
func CreateJwtToken(uuid string) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
@ -15,7 +17,7 @@ func CreateJwtToken(uuid string) (string, error) {
"exp": time.Now().Add(time.Hour * 24).Unix(),
})
tokenString, err := token.SignedString(secretKey)
tokenString, err := token.SignedString(deafultSecretKey)
if err != nil {
return "", err
}
@ -23,13 +25,24 @@ func CreateJwtToken(uuid string) (string, error) {
return tokenString, nil
}
func VerifyJwtToken(tokenString string) error {
func VerifyJwtToken(tokenString string, customKey ...[]byte) error {
secretKey := deafultSecretKey
if len(customKey) > 0 {
if len(customKey[0]) < 32 {
return errors.New("provided key is too short, minimum length is 32 bytes")
}
secretKey = customKey[0]
}
if len(secretKey) == 0 {
return errors.New("no secret key available")
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
// TODO: CHANGE SECRET KEY
return secretKey, nil
})

View File

@ -11,4 +11,3 @@ CREATE TABLE users (
);
CREATE UNIQUE INDEX idx_users_uuid ON users(uuid);
CREATE UNIQUE INDEX idx_users_username ON users(username);

View File

@ -0,0 +1,48 @@
import axios from "axios";
import axiosInstance from "./config";
export interface LoginResponse {
token: string;
message?: string;
}
export interface RegisterResponse {
token: string;
message?: string;
}
/**
* Registers a new user by sending a POST request to the backend.
*/
export async function registerUser(email: string, password: string): Promise<RegisterResponse> {
try {
const response = await axiosInstance.post("/auth/register", {
Email: email,
Password: password,
});
return response.data;
} catch (error: any) {
if (axios.isAxiosError(error)) {
throw new Error(error.response?.data?.message || "Failed to register.");
}
throw error;
}
}
/**
* Logs in a user by sending a POST request to the backend.
*/
export async function loginUser(email: string, password: string): Promise<LoginResponse> {
try {
const response = await axiosInstance.post("/auth/login", {
Email: email,
Password: password,
});
return response.data;
} catch (error: any) {
if (axios.isAxiosError(error)) {
throw new Error(error.response?.data?.message || "Failed to log in.");
}
throw error;
}
}

28
frontend/api/config.ts Normal file
View File

@ -0,0 +1,28 @@
import axios from "axios";
const axiosInstance = axios.create({
baseURL: process.env.NEXT_PUBLIC_BACKEND_URL || "http://localhost:8000",
headers: {
"Content-Type": "application/json",
},
});
axiosInstance.interceptors.request.use(
(config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);
axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
return Promise.reject(error);
}
);
export default axiosInstance;

View File

@ -0,0 +1,9 @@
import Image from "next/image";
export function GoogleSigninButton() {
return (
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer ">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
</div>
);
}

View File

@ -1,3 +1,10 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { signInSchema } from "@/schema/authSchema";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
@ -6,19 +13,65 @@ import ForgotPasswordModal from "./forgot-password-modal";
import Link from "next/link";
import Image from "next/image";
import { GoogleSigninButton } from "./google-oauth";
import { z } from "zod";
import { useContext, useState } from "react";
import { useRouter } from "next/navigation";
import { loginUser } from "@/api/authentication";
import { SessionContext } from "@/context/SessionContext";
export default function SigninPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const session = useContext(SessionContext);
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(signInSchema),
defaultValues: {
email: "",
password: "",
},
});
const onSubmit = async (values: z.infer<typeof signInSchema>) => {
setServerError(null);
setIsLoading(true);
try {
const data = await loginUser(values.email, values.password);
if (!data) {
setServerError("An error occurred while logging in. Please try again.");
throw new Error("No data received from the server.");
}
session!.setToken(data.token);
session!.setUser(values.email);
router.push("/setup");
} catch (error: any) {
console.error("Error logging in:", error);
setServerError(error.message);
} finally {
setIsLoading(false);
}
};
return (
<div>
<div className="grid grid-cols-[0.7fr_1.2fr] h-screen overflow-hidden">
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<div className="flex justify-center items-center">
{/* login box */}
<div className="container px-[25%]">
<div className="flex flex-col justify-center items-center">
<span>
<Image src={`/forfarm-logo.png`} alt="Forfarm" width={150} height={150}></Image>
<Image src="/forfarm-logo.png" alt="Forfarm" width={150} height={150} />
</span>
<h1 className="text-3xl font-semibold">Welcome back.</h1>
<div className="flex whitespace-nowrap gap-x-2 mt-2">
@ -31,27 +84,28 @@ export default function SigninPage() {
</div>
</div>
<div className="flex flex-col mt-4">
{/* Sign in form */}
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col mt-4">
<div>
<Label htmlFor="email">Email</Label>
<Input type="email" id="email" placeholder="Email" />
<Input type="email" id="email" placeholder="Email" {...register("email")} />
{errors.email && <p className="text-red-600 text-sm">{errors.email.message}</p>}
</div>
<div className="mt-5">
<div>
<Label htmlFor="password">Password</Label>
<Input type="empasswordail" id="password" placeholder="Password" />
</div>
<Input type="password" id="password" placeholder="Password" {...register("password")} />
{errors.password && <p className="text-red-600 text-sm">{errors.password.message}</p>}
</div>
<Button className="mt-5 rounded-full">Log in</Button>
</div>
<Button type="submit" className="mt-5 rounded-full" disabled={isLoading}>
{isLoading ? "Logging in..." : "Log in"}
</Button>
</form>
<div id="signin-footer" className="flex justify-between mt-5">
<div className="flex items-center space-x-2">
<Checkbox id="terms" />
<label
htmlFor="terms"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
<label htmlFor="terms" className="text-sm font-medium leading-none">
Remember me
</label>
</div>
@ -60,12 +114,8 @@ export default function SigninPage() {
<div className="my-5">
<p className="text-sm">Or log in with</p>
{/* OAUTH */}
<div className="flex flex-col gap-x-5 mt-3">
{/* Google */}
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer ">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
</div>
<GoogleSigninButton />
</div>
</div>
</div>

View File

@ -1,61 +1,127 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
import { Button } from "@/components/ui/button";
import { signUpSchema } from "@/schema/authSchema";
import Link from "next/link";
import Image from "next/image";
import { useContext, useState } from "react";
import { z } from "zod";
import { useRouter } from "next/navigation";
import { registerUser } from "@/api/authentication";
import { SessionContext } from "@/context/SessionContext";
export default function SignupPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [successMessage, setSuccessMessage] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const session = useContext(SessionContext);
const {
register,
handleSubmit,
formState: { errors },
} = useForm<z.infer<typeof signUpSchema>>({
resolver: zodResolver(signUpSchema),
defaultValues: {
email: "",
password: "",
confirmPassword: "",
},
});
const onSubmit = async (values: z.infer<typeof signUpSchema>) => {
setServerError(null);
setSuccessMessage(null);
setIsLoading(true);
try {
const data = await registerUser(values.email, values.password);
if (!data) {
setServerError("An error occurred while registering. Please try again.");
throw new Error("No data received from the server.");
}
session!.setToken(data.token);
session!.setUser(values.email);
setSuccessMessage("Registration successful! You can now sign in.");
router.push("/setup");
} catch (error: any) {
console.error("Error during registration:", error);
setServerError(error.message);
} finally {
setIsLoading(false);
}
};
return (
<div>
<div className="grid grid-cols-[0.7fr_1.2fr] h-screen overflow-hidden">
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<div className="flex justify-center items-center">
{/* login box */}
<div className="container px-[25%]">
<div className="flex flex-col justify-center items-center">
<span>
<Image src={`/forfarm-logo.png`} alt="Forfarm" width={150} height={150}></Image>
<Image src="/forfarm-logo.png" alt="Forfarm" width={150} height={150} />
</span>
<h1 className="text-3xl font-semibold">Hi! Welcome</h1>
<div className="flex whitespace-nowrap gap-x-2 mt-2">
<span className="text-md">Already have accounts?</span>
<span className="text-md">Already have an account?</span>
<span className="text-green-600">
<Link href="signin" className="underline">
<Link href="/auth/signin" className="underline">
Sign in
</Link>
</span>
</div>
</div>
<div className="flex flex-col mt-4">
{/* Signup form */}
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col mt-4">
<div>
<Label htmlFor="email">Email</Label>
<Input type="email" id="email" placeholder="Email" />
<Input type="email" id="email" placeholder="Email" {...register("email")} />
{errors.email && <p className="text-red-600 text-sm">{errors.email.message}</p>}
</div>
<div className="mt-5">
<div>
<Label htmlFor="password">Password</Label>
<Input type="empasswordail" id="password" placeholder="Password" />
</div>
<Input type="password" id="password" placeholder="Password" {...register("password")} />
{errors.password && <p className="text-red-600 text-sm">{errors.password.message}</p>}
</div>
<div className="mt-5">
<div>
<Label htmlFor="password">Confirm Password</Label>
<Input type="empasswordail" id="password" placeholder="Password" />
</div>
<Label htmlFor="confirmPassword">Confirm Password</Label>
<Input
type="password"
id="confirmPassword"
placeholder="Confirm Password"
{...register("confirmPassword")}
/>
{errors.confirmPassword && <p className="text-red-600 text-sm">{errors.confirmPassword.message}</p>}
</div>
<Button className="mt-5 rounded-full">Sign up</Button>
</div>
{serverError && <p className="text-red-600 mt-2 text-sm">{serverError}</p>}
{successMessage && <p className="text-green-600 mt-2 text-sm">{successMessage}</p>}
<Button type="submit" className="mt-5 rounded-full" disabled={isLoading}>
{isLoading ? "Signing up..." : "Sign up"}
</Button>
</form>
<div className="my-5">
<p className="text-sm">Or log in with</p>
{/* OAUTH */}
<p className="text-sm">Or sign up with</p>
<div className="flex flex-col gap-x-5 mt-3">
{/* Google */}
{/* Google OAuth button or additional providers */}
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
</div>

View File

@ -3,6 +3,8 @@ import { Open_Sans, Roboto_Mono } from "next/font/google";
import "./globals.css";
import { ThemeProvider } from "@/components/theme-provider";
import { SessionProvider } from "@/context/SessionContext";
const openSans = Open_Sans({
subsets: ["latin"],
display: "swap",
@ -33,13 +35,15 @@ export default function RootLayout({
return (
<html lang="en" suppressHydrationWarning>
<head />
<body className={`${openSans.variable} ${robotoMono.variable} font-sans antialiased`}>
<SessionProvider>
<body className={`${openSans.variable} ${robotoMono.variable} antialiased`}>
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<div className="relative flex min-h-screen flex-col">
<div className="flex-1 bg-background">{children}</div>
</div>
</ThemeProvider>
</body>
</SessionProvider>
</html>
);
}

View File

@ -41,7 +41,7 @@ export default function Home() {
It's a smart and easy way to optimize your agricultural business, with the help of AI-driven insights and
real-time data.
</p>
<Link href="/auth/signin">
<Link href="/setup">
<Button className="bg-black text-white text-md font-bold px-4 py-6 rounded-full hover:bg-gray-600">
Manage your farm
</Button>

View File

@ -1,4 +1,5 @@
import { AppSidebar } from "@/components/app-sidebar";
import { AppSidebar } from "@/components/sidebar/app-sidebar";
import { ThemeToggle } from "@/components/theme-toggle";
import {
Breadcrumb,
BreadcrumbItem,
@ -22,6 +23,7 @@ export default function AppLayout({
<header className="flex h-16 shrink-0 items-center gap-2">
<div className="flex items-center gap-2 px-4">
<SidebarTrigger className="-ml-1" />
<ThemeToggle />
<Separator orientation="vertical" className="mr-2 h-4" />
<Breadcrumb>
<BreadcrumbList>

View File

@ -0,0 +1,12 @@
"use client";
import React, { ReactNode } from "react";
import { SessionProvider } from "next-auth/react";
interface Props {
children: ReactNode;
}
export function SessesionProviderClient(props: Props) {
return <SessionProvider>{props.children}</SessionProvider>;
}

View File

@ -1,144 +0,0 @@
"use client";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
} from "@/components/ui/sidebar";
import {
Calendar,
Home,
Settings,
Sun,
Moon,
LogOut,
Wrench,
FileText,
Bot,
Factory,
Store,
} from "lucide-react";
import { usePathname } from "next/navigation";
import { useState } from "react";
import Image from "next/image";
export function AppSidebar() {
const pathname = usePathname();
const [darkMode, setDarkMode] = useState(false);
const items = [
{
title: "Dashboard",
url: "#",
icon: Home,
},
{
title: "SetUp",
url: "#",
icon: Wrench,
},
{
title: "Management",
url: "#",
icon: Calendar,
},
{
title: "Work Order Management",
url: "#",
icon: FileText,
},
{
title: "AI-Chatbot",
url: "#",
icon: Bot,
},
{
title: "Inventory Management",
url: "#",
icon: Factory,
},
{
title: "Marketplace",
url: "#",
icon: Store,
},
{
title: "Settings",
url: "#",
icon: Settings,
},
];
return (
<Sidebar className="w-64 h-screen bg-gray-100 border-r border-gray-300 shadow-md flex flex-col justify-between">
{/* Menu Items */}
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel className="text-lg font-semibold text-gray-700 px-4 py-3">
<div className="flex items-center gap-x-2 mt-14">
<Image
src="/forfarm-logo.png"
width={80}
height={80}
alt="ForFarm Logo"
className="w-24 h-24 rounded-full"
/>
<h1 className="text-xl">ForFarm</h1>
</div>
</SidebarGroupLabel>
<SidebarGroupContent className="flex flex-col flex-grow justify-center mt-24">
<SidebarMenu>
{items.map((item) => {
const isActive = pathname === item.url;
return (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton asChild>
<a
href={item.url}
className={`flex h-14 items-center gap-3 my-2 px-4 py-3 rounded-lg text-gray-700 transition duration-300 ${
isActive
? "bg-blue-500 text-white font-semibold"
: "hover:bg-gray-200"
}`}
>
<item.icon />
<span>{item.title}</span>
</a>
</SidebarMenuButton>
</SidebarMenuItem>
);
})}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
{/* Bottom Section: Theme Toggle & Logout */}
<div className="p-4 border-t border-gray-300">
{/* Theme Toggle */}
<button
className="flex items-center gap-3 w-full px-4 py-3 rounded-lg transition duration-300 hover:bg-gray-200"
onClick={() => setDarkMode(!darkMode)}
>
{darkMode ? <Sun size={18} /> : <Moon size={18} />}
<span>{darkMode ? "Light Mode" : "Dark Mode"}</span>
</button>
{/* Logout Button */}
<button
className="flex items-center gap-3 w-full px-4 py-3 rounded-lg text-red-500 transition duration-300 hover:bg-red-100 mt-2"
onClick={() => alert("Logging out...")} // Replace with actual logout function
>
<LogOut size={18} />
<span>Logout</span>
</button>
</div>
</Sidebar>
);
}

View File

@ -0,0 +1,152 @@
"use client";
import * as React from "react";
import {
AudioWaveform,
BookOpen,
Bot,
Command,
Frame,
GalleryVerticalEnd,
Map,
PieChart,
Settings2,
SquareTerminal,
} from "lucide-react";
import { NavMain } from "./nav-main";
import { NavProjects } from "./nav-projects";
import { NavUser } from "./nav-user";
import { TeamSwitcher } from "./team-switcher";
import { Sidebar, SidebarContent, SidebarFooter, SidebarHeader, SidebarRail } from "@/components/ui/sidebar";
const data = {
user: {
name: "shadcn",
email: "m@example.com",
avatar: "/avatars/shadcn.jpg",
},
teams: [
{
name: "Farm 1",
logo: GalleryVerticalEnd,
plan: "Hatyai",
},
{
name: "Farm 2",
logo: AudioWaveform,
plan: "Songkla",
},
{
name: "Farm 3",
logo: Command,
plan: "Layong",
},
],
navMain: [
{
title: "Dashboard",
url: "#",
icon: SquareTerminal,
isActive: true,
items: [
{
title: "Analytic",
url: "#",
},
],
},
{
title: "AI Chatbot",
url: "#",
icon: Bot,
items: [
{
title: "Main model",
url: "#",
},
],
},
{
title: "Documentation",
url: "#",
icon: BookOpen,
items: [
{
title: "Introduction",
url: "#",
},
{
title: "Get Started",
url: "#",
},
{
title: "Tutorials",
url: "#",
},
{
title: "Changelog",
url: "#",
},
],
},
{
title: "Settings",
url: "#",
icon: Settings2,
items: [
{
title: "General",
url: "#",
},
{
title: "Team",
url: "#",
},
{
title: "Billing",
url: "#",
},
{
title: "Limits",
url: "#",
},
],
},
],
projects: [
{
name: "Crops 1",
url: "#",
icon: Frame,
},
{
name: "Crops 2",
url: "#",
icon: PieChart,
},
{
name: "Crops 3",
url: "#",
icon: Map,
},
],
};
export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
return (
<Sidebar collapsible="icon" {...props}>
<SidebarHeader>
<TeamSwitcher teams={data.teams} />
</SidebarHeader>
<SidebarContent>
<NavMain items={data.navMain} />
<NavProjects projects={data.projects} />
</SidebarContent>
<SidebarFooter>
<NavUser user={data.user} />
</SidebarFooter>
<SidebarRail />
</Sidebar>
);
}

View File

@ -0,0 +1,64 @@
"use client";
import { ChevronRight, type LucideIcon } from "lucide-react";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
import {
SidebarGroup,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
} from "@/components/ui/sidebar";
export function NavMain({
items,
}: {
items: {
title: string;
url: string;
icon?: LucideIcon;
isActive?: boolean;
items?: {
title: string;
url: string;
}[];
}[];
}) {
return (
<SidebarGroup>
<SidebarGroupLabel>Platform</SidebarGroupLabel>
<SidebarMenu>
{items.map((item) => (
<Collapsible key={item.title} asChild defaultOpen={item.isActive} className="group/collapsible">
<SidebarMenuItem>
<CollapsibleTrigger asChild>
<SidebarMenuButton tooltip={item.title}>
{item.icon && <item.icon />}
<span>{item.title}</span>
<ChevronRight className="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
</SidebarMenuButton>
</CollapsibleTrigger>
<CollapsibleContent>
<SidebarMenuSub>
{item.items?.map((subItem) => (
<SidebarMenuSubItem key={subItem.title}>
<SidebarMenuSubButton asChild>
<a href={subItem.url}>
<span>{subItem.title}</span>
</a>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
</SidebarMenuSub>
</CollapsibleContent>
</SidebarMenuItem>
</Collapsible>
))}
</SidebarMenu>
</SidebarGroup>
);
}

View File

@ -0,0 +1,82 @@
"use client";
import { Folder, Forward, MoreHorizontal, Trash2, type LucideIcon } from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
SidebarGroup,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuAction,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from "@/components/ui/sidebar";
export function NavProjects({
projects,
}: {
projects: {
name: string;
url: string;
icon: LucideIcon;
}[];
}) {
const { isMobile } = useSidebar();
return (
<SidebarGroup className="group-data-[collapsible=icon]:hidden">
<SidebarGroupLabel>Projects</SidebarGroupLabel>
<SidebarMenu>
{projects.map((item) => (
<SidebarMenuItem key={item.name}>
<SidebarMenuButton asChild>
<a href={item.url}>
<item.icon />
<span>{item.name}</span>
</a>
</SidebarMenuButton>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuAction showOnHover>
<MoreHorizontal />
<span className="sr-only">More</span>
</SidebarMenuAction>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-48 rounded-lg"
side={isMobile ? "bottom" : "right"}
align={isMobile ? "end" : "start"}>
<DropdownMenuItem>
<Folder className="text-muted-foreground" />
<span>View Project</span>
</DropdownMenuItem>
<DropdownMenuItem>
<Forward className="text-muted-foreground" />
<span>Share Project</span>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>
<Trash2 className="text-muted-foreground" />
<span>Delete Project</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
))}
<SidebarMenuItem>
<SidebarMenuButton className="text-sidebar-foreground/70">
<MoreHorizontal className="text-sidebar-foreground/70" />
<span>More</span>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
);
}

View File

@ -0,0 +1,100 @@
"use client";
import { BadgeCheck, Bell, ChevronsUpDown, CreditCard, LogOut, Sparkles } from "lucide-react";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar } from "@/components/ui/sidebar";
import { useLogout } from "@/hooks/useLogout";
export function NavUser({
user,
}: {
user: {
name: string;
email: string;
avatar: string;
};
}) {
const { isMobile } = useSidebar();
const logout = useLogout();
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">{user.name}</span>
<span className="truncate text-xs">{user.email}</span>
</div>
<ChevronsUpDown className="ml-auto size-4" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg"
side={isMobile ? "bottom" : "right"}
align="end"
sideOffset={4}>
<DropdownMenuLabel className="p-0 font-normal">
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">{user.name}</span>
<span className="truncate text-xs">{user.email}</span>
</div>
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem>
<Sparkles />
Upgrade to Pro
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem>
<BadgeCheck />
Account
</DropdownMenuItem>
<DropdownMenuItem>
<CreditCard />
Billing
</DropdownMenuItem>
<DropdownMenuItem>
<Bell />
Notifications
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={logout}>
<LogOut />
Log out
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
);
}

View File

@ -0,0 +1,74 @@
"use client";
import * as React from "react";
import { ChevronsUpDown, Plus } from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar } from "@/components/ui/sidebar";
export function TeamSwitcher({
teams,
}: {
teams: {
name: string;
logo: React.ElementType;
plan: string;
}[];
}) {
const { isMobile } = useSidebar();
const [activeTeam, setActiveTeam] = React.useState(teams[0]);
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground">
<div className="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground">
<activeTeam.logo className="size-4" />
</div>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">{activeTeam.name}</span>
<span className="truncate text-xs">{activeTeam.plan}</span>
</div>
<ChevronsUpDown className="ml-auto" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg"
align="start"
side={isMobile ? "bottom" : "right"}
sideOffset={4}>
<DropdownMenuLabel className="text-xs text-muted-foreground">Teams</DropdownMenuLabel>
{teams.map((team, index) => (
<DropdownMenuItem key={team.name} onClick={() => setActiveTeam(team)} className="gap-2 p-2">
<div className="flex size-6 items-center justify-center rounded-sm border">
<team.logo className="size-4 shrink-0" />
</div>
{team.name}
<DropdownMenuShortcut>{index + 1}</DropdownMenuShortcut>
</DropdownMenuItem>
))}
<DropdownMenuSeparator />
<DropdownMenuItem className="gap-2 p-2">
<div className="flex size-6 items-center justify-center rounded-md border bg-background">
<Plus className="size-4" />
</div>
<div className="font-medium text-muted-foreground">Add team</div>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
);
}

View File

@ -0,0 +1,11 @@
"use client"
import * as CollapsiblePrimitive from "@radix-ui/react-collapsible"
const Collapsible = CollapsiblePrimitive.Root
const CollapsibleTrigger = CollapsiblePrimitive.CollapsibleTrigger
const CollapsibleContent = CollapsiblePrimitive.CollapsibleContent
export { Collapsible, CollapsibleTrigger, CollapsibleContent }

View File

@ -0,0 +1,64 @@
"use client";
import React, { createContext, useContext, useState, useEffect, ReactNode } from "react";
import Cookies from "js-cookie";
interface SessionContextType {
token: string | null;
user: any | null;
setToken: (token: string | null) => void;
setUser: (user: any | null) => void;
loading: boolean;
}
const SessionContext = createContext<SessionContextType | undefined>(undefined);
interface SessionProviderProps {
children: ReactNode;
}
export function SessionProvider({ children }: SessionProviderProps) {
const [token, setTokenState] = useState<string | null>(null);
const [user, setUserState] = useState<any | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const setToken = (newToken: string | null) => {
if (newToken) {
Cookies.set("token", newToken, { expires: 7 });
} else {
Cookies.remove("token");
}
setTokenState(newToken);
};
const setUser = (newUser: any | null) => {
if (newUser) {
localStorage.setItem("user", JSON.stringify(newUser));
} else {
localStorage.removeItem("user");
}
setUserState(newUser);
};
useEffect(() => {
const storedToken = Cookies.get("token") || null;
const storedUser = localStorage.getItem("user");
if (storedToken) {
setTokenState(storedToken);
}
if (storedUser) {
try {
setUserState(JSON.parse(storedUser));
} catch (error) {
console.error("Failed to parse stored user.", error);
}
}
setLoading(false);
}, []);
return (
<SessionContext.Provider value={{ token, user, setToken, setUser, loading }}>{children}</SessionContext.Provider>
);
}
export { SessionContext };

View File

@ -0,0 +1,30 @@
"use client";
import { useContext } from "react";
import { useRouter } from "next/navigation";
import { SessionContext } from "@/context/SessionContext";
import Cookies from "js-cookie";
export function useLogout() {
const router = useRouter();
const context = useContext(SessionContext);
if (!context) {
throw new Error("useLogout must be used within a SessionProvider");
}
const { setToken, setUser } = context;
const logout = () => {
Cookies.remove("token");
Cookies.remove("user");
setToken(null);
setUser(null);
console.log(Cookies.get("token"));
router.push("/");
};
return logout;
}

View File

@ -0,0 +1,18 @@
"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useSession } from "./useSession";
export function useProtectedRoute() {
const { data: session } = useSession();
const router = useRouter();
useEffect(() => {
if (!session?.token) {
router.push("/signin");
}
}, [session?.token, router]);
return session?.token;
}

View File

@ -0,0 +1,22 @@
"use client";
import { useContext } from "react";
import { SessionContext } from "@/context/SessionContext";
export function useSession() {
const context = useContext(SessionContext);
if (!context) {
throw new Error("useSession must be used within a SessionProvider");
}
const { token, user, loading } = context;
let status: "loading" | "authenticated" | "unauthenticated";
if (loading) status = "loading";
else if (token) status = "authenticated";
else status = "unauthenticated";
const session = token ? { token, user } : null;
return { data: session, status };
}

View File

@ -12,6 +12,7 @@
"@hookform/resolvers": "^4.0.0",
"@radix-ui/react-avatar": "^1.1.3",
"@radix-ui/react-checkbox": "^1.1.4",
"@radix-ui/react-collapsible": "^1.1.3",
"@radix-ui/react-dialog": "^1.1.6",
"@radix-ui/react-dropdown-menu": "^2.1.6",
"@radix-ui/react-label": "^2.1.2",
@ -23,10 +24,13 @@
"@react-google-maps/api": "^2.20.6",
"@tailwindcss/typography": "^0.5.16",
"@tanstack/react-query": "^5.66.0",
"axios": "^1.7.9",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"js-cookie": "^3.0.5",
"lucide-react": "^0.475.0",
"next": "15.1.0",
"next-auth": "^4.24.11",
"next-themes": "^0.4.4",
"react": "^19.0.0",
"react-dom": "^19.0.0",

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
import { z } from "zod";
export const signInSchema = z.object({
email: z
.string({ required_error: "Email is required" })
.min(1, { message: "Email is required" })
.email({ message: "Invalid email address" }),
password: z
.string({ required_error: "Password is required" })
.min(6, { message: "Password must be at least 6 characters long" }),
});
export const signUpSchema = z
.object({
email: z
.string({ required_error: "Email is required" })
.min(1, { message: "Email is required" })
.email({ message: "Invalid email address" }),
password: z
.string({ required_error: "Password is required" })
.min(6, { message: "Password must be at least 6 characters" }),
confirmPassword: z
.string({ required_error: "Confirm your password" })
.min(6, { message: "Confirm Password must be at least 6 characters" }),
})
.refine((data) => data.password === data.confirmPassword, {
message: "Passwords do not match",
path: ["confirmPassword"],
});

5
package.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": {
"@types/js-cookie": "^3.0.6"
}
}

22
pnpm-lock.yaml Normal file
View File

@ -0,0 +1,22 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.:
dependencies:
'@types/js-cookie':
specifier: ^3.0.6
version: 3.0.6
packages:
'@types/js-cookie@3.0.6':
resolution: {integrity: sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==}
snapshots:
'@types/js-cookie@3.0.6': {}