swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
265 lines · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
---
import SettingsLayout from "@/layouts/SettingsLayout.astro";
import { MFASetup } from "@/components/settings/MFASetup";

const user = Astro.locals.user;

if (!user) {
  return Astro.redirect("/login");
}
---

<SettingsLayout title="Security Settings">
  <div class="space-y-6">
    <!-- Header -->
    <div>
      <h3 class="text-xl font-bold text-white flex items-center gap-3">
        <div
          class="p-2 rounded-lg bg-gradient-to-br from-orange-500/20 to-red-500/20 border border-orange-500/30"
        >
          <svg
            class="h-5 w-5 text-orange-400"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="2"
              d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"
            ></path>
          </svg>
        </div>
        Security Settings
      </h3>
      <p class="text-gray-500 text-sm mt-2 ml-12">
        Manage your security preferences and two-factor authentication.
      </p>
    </div>

    <!-- 2FA Section -->
    <div class="relative">
      <div
        class="absolute -inset-[1px] bg-gradient-to-r from-orange-500/20 via-transparent to-red-500/20 rounded-xl opacity-50"
      >
      </div>
      <div
        class="relative rounded-xl border border-white/10 bg-[#0d1117]/80 backdrop-blur-sm p-6"
      >
        <h4
          class="text-lg font-semibold text-white mb-4 flex items-center gap-2"
        >
          <svg
            class="h-5 w-5 text-orange-400"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="2"
              d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"
            ></path>
          </svg>
          Two-Factor Authentication
        </h4>

        {
          user.twoFactorEnabled ? (
            <div class="rounded-lg bg-green-500/10 border border-green-500/30 p-4">
              <div class="flex items-start gap-3">
                <div class="p-1.5 rounded-full bg-green-500/20">
                  <svg
                    class="h-4 w-4 text-green-400"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M5 13l4 4L19 7"
                    />
                  </svg>
                </div>
                <div class="flex-1">
                  <p class="text-green-400 font-medium">
                    Two-factor authentication is enabled
                  </p>
                  <p class="text-gray-500 text-sm mt-1">
                    Your account is protected with an additional layer of
                    security.
                  </p>
                </div>
              </div>
              <button
                type="button"
                id="disable-2fa-btn"
                class="mt-4 inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-red-500/10 border border-red-500/30 text-sm font-medium text-red-400 hover:bg-red-500/20 transition-colors"
              >
                <svg
                  class="h-4 w-4"
                  fill="none"
                  stroke="currentColor"
                  viewBox="0 0 24 24"
                >
                  <path
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    stroke-width="2"
                    d="M6 18L18 6M6 6l12 12"
                  />
                </svg>
                Disable 2FA
              </button>
            </div>
          ) : (
            <MFASetup client:load />
          )
        }
      </div>
    </div>

    <!-- Password Section -->
    <div class="relative">
      <div
        class="absolute -inset-[1px] bg-gradient-to-r from-purple-500/20 via-transparent to-pink-500/20 rounded-xl opacity-50"
      >
      </div>
      <div
        class="relative rounded-xl border border-white/10 bg-[#0d1117]/80 backdrop-blur-sm p-6"
      >
        <h4
          class="text-lg font-semibold text-white mb-4 flex items-center gap-2"
        >
          <svg
            class="h-5 w-5 text-purple-400"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
          >
            <path
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="2"
              d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"
            ></path>
          </svg>
          Change Password
        </h4>
        <form id="password-form" class="space-y-4">
          <div>
            <label class="block text-sm font-medium text-gray-300 mb-2"
              >Current Password</label
            >
            <input
              type="password"
              name="currentPassword"
              required
              class="w-full max-w-md rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white placeholder:text-gray-600 focus:outline-none focus:border-purple-500/50 focus:ring-2 focus:ring-purple-500/20 transition-all"
            />
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-300 mb-2"
              >New Password</label
            >
            <input
              type="password"
              name="newPassword"
              required
              minlength="8"
              class="w-full max-w-md rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white placeholder:text-gray-600 focus:outline-none focus:border-purple-500/50 focus:ring-2 focus:ring-purple-500/20 transition-all"
            />
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-300 mb-2"
              >Confirm New Password</label
            >
            <input
              type="password"
              name="confirmPassword"
              required
              class="w-full max-w-md rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-white placeholder:text-gray-600 focus:outline-none focus:border-purple-500/50 focus:ring-2 focus:ring-purple-500/20 transition-all"
            />
          </div>
          <button
            type="submit"
            class="inline-flex items-center gap-2 px-4 py-2.5 rounded-lg bg-gradient-to-r from-purple-500 to-pink-600 text-sm font-medium text-white shadow-lg shadow-purple-500/25 hover:shadow-purple-500/40 transition-all"
          >
            <svg
              class="h-4 w-4"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M5 13l4 4L19 7"></path>
            </svg>
            Update Password
          </button>
        </form>
      </div>
    </div>
  </div>
</SettingsLayout>

<script>
  const disableBtn = document.getElementById("disable-2fa-btn");

  disableBtn?.addEventListener("click", async () => {
    if (confirm("Are you sure you want to disable 2FA?")) {
      try {
        const response = await fetch("/api/auth/2fa/disable", {
          method: "POST",
        });
        if (response.ok) {
          window.location.reload();
        } else {
          alert("Failed to disable 2FA");
        }
      } catch (error) {
        console.error("Error disabling 2FA:", error);
      }
    }
  });

  // Password form logic logic remains same but simplified since most logic was inline or simple
  const form = document.getElementById("password-form") as HTMLFormElement;
  form?.addEventListener("submit", async (e) => {
    e.preventDefault();
    const formData = new FormData(form);
    const data = Object.fromEntries(formData);

    // Check match
    if (data.newPassword !== data.confirmPassword) {
      alert("New passwords do not match");
      return;
    }

    try {
      const response = await fetch("/api/auth/password", {
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data),
      });

      if (response.ok) {
        alert("Password updated successfully");
        form.reset();
      } else {
        const result = await response.json();
        alert(result.message || "Failed to update password");
      }
    } catch (error) {
      console.error("Error updating password:", error);
      alert("An error occurred");
    }
  });
</script>